Quantcast

how to convert character variables into numeric variables directly

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

how to convert character variables into numeric variables directly

Xumin Zeng
Here is the example.

> age=18:29
> height=c(76.1,77,78.1,78.2,78.8,79.7,79.9,81.1,81.2,81.8,82.8,83.5)
> type=c("A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D")
> typec=c("0","4","2","9","0","7","2","3","0","1","2","3")
> typen=c(0,1,2,3,0,1,2,3,0,1,2,3)
> data1=data.frame(age=age,height=height, type=type, typec=typec,
typen=typen)

> data1[,3]=as.numeric(data1[,3])
> data1[,4]=as.numeric(data1[,4])
> data1[,5]=as.numeric(data1[,5])

> print(data1)

and I got the output as:

   age height type typec typen
1   18   76.1    1     1     0
2   19   77.0    2     5     1
3   20   78.1    3     3     2
4   21   78.2    4     7     3
5   22   78.8    1     1     0
6   23   79.7    2     6     1
7   24   79.9    3     3     2
8   25   81.1    4     4     3
9   26   81.2    1     1     0
10  27   81.8    2     2     1
11  28   82.8    3     3     2
12  29   83.5    4     4     3

The "typec" is not what I expected. How can I get the direct conversion
from character to numeric and get the following output?

   age height type typec typen
1   18   76.1    1     0     0
2   19   77.0    2     4     1
3   20   78.1    3     2     2
4   21   78.2    4     9     3
5   22   78.8    1     0     0
6   23   79.7    2     7     1
7   24   79.9    3     2     2
8   25   81.1    4     3     3
9   26   81.2    1     0     0
10  27   81.8    2     1     1
11  28   82.8    3     2     2
12  29   83.5    4     3     3

Thanks.

Xumin
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to convert character variables into numeric variables directly

milton ruser
Try: as.numeric(as.character( typec))

milton
On Mon, Mar 8, 2010 at 12:55 PM, Xumin Zeng <[hidden email]> wrote:

> Here is the example.
>
> > age=18:29
> > height=c(76.1,77,78.1,78.2,78.8,79.7,79.9,81.1,81.2,81.8,82.8,83.5)
> > type=c("A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D")
> > typec=c("0","4","2","9","0","7","2","3","0","1","2","3")
> > typen=c(0,1,2,3,0,1,2,3,0,1,2,3)
> > data1=data.frame(age=age,height=height, type=type, typec=typec,
> typen=typen)
>
> > data1[,3]=as.numeric(data1[,3])
> > data1[,4]=as.numeric(data1[,4])
> > data1[,5]=as.numeric(data1[,5])
>
> > print(data1)
>
> and I got the output as:
>
>   age height type typec typen
> 1   18   76.1    1     1     0
> 2   19   77.0    2     5     1
> 3   20   78.1    3     3     2
> 4   21   78.2    4     7     3
> 5   22   78.8    1     1     0
> 6   23   79.7    2     6     1
> 7   24   79.9    3     3     2
> 8   25   81.1    4     4     3
> 9   26   81.2    1     1     0
> 10  27   81.8    2     2     1
> 11  28   82.8    3     3     2
> 12  29   83.5    4     4     3
>
> The "typec" is not what I expected. How can I get the direct conversion
> from character to numeric and get the following output?
>
>   age height type typec typen
> 1   18   76.1    1     0     0
> 2   19   77.0    2     4     1
> 3   20   78.1    3     2     2
> 4   21   78.2    4     9     3
> 5   22   78.8    1     0     0
> 6   23   79.7    2     7     1
> 7   24   79.9    3     2     2
> 8   25   81.1    4     3     3
> 9   26   81.2    1     0     0
> 10  27   81.8    2     1     1
> 11  28   82.8    3     2     2
> 12  29   83.5    4     3     3
>
> Thanks.
>
> Xumin
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to convert character variables into numeric variables directly

Xumin Zeng
Thanks, it works!

Xumin




milton ruser <[hidden email]>
03/08/2010 01:02 PM

To
Xumin Zeng <[hidden email]>
cc
r-help <[hidden email]>
Subject
Re: [R] how to convert character variables into numeric variables directly






Try: as.numeric(as.character( typec))

milton
On Mon, Mar 8, 2010 at 12:55 PM, Xumin Zeng <[hidden email]> wrote:
Here is the example.

> age=18:29
> height=c(76.1,77,78.1,78.2,78.8,79.7,79.9,81.1,81.2,81.8,82.8,83.5)
> type=c("A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D")
> typec=c("0","4","2","9","0","7","2","3","0","1","2","3")
> typen=c(0,1,2,3,0,1,2,3,0,1,2,3)
> data1=data.frame(age=age,height=height, type=type, typec=typec,
typen=typen)

> data1[,3]=as.numeric(data1[,3])
> data1[,4]=as.numeric(data1[,4])
> data1[,5]=as.numeric(data1[,5])

> print(data1)

and I got the output as:

  age height type typec typen
1   18   76.1    1     1     0
2   19   77.0    2     5     1
3   20   78.1    3     3     2
4   21   78.2    4     7     3
5   22   78.8    1     1     0
6   23   79.7    2     6     1
7   24   79.9    3     3     2
8   25   81.1    4     4     3
9   26   81.2    1     1     0
10  27   81.8    2     2     1
11  28   82.8    3     3     2
12  29   83.5    4     4     3

The "typec" is not what I expected. How can I get the direct conversion
from character to numeric and get the following output?

  age height type typec typen
1   18   76.1    1     0     0
2   19   77.0    2     4     1
3   20   78.1    3     2     2
4   21   78.2    4     9     3
5   22   78.8    1     0     0
6   23   79.7    2     7     1
7   24   79.9    3     2     2
8   25   81.1    4     3     3
9   26   81.2    1     0     0
10  27   81.8    2     1     1
11  28   82.8    3     2     2
12  29   83.5    4     3     3

Thanks.

Xumin
       [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Odp: how to convert character variables into numeric variables directly

PIKAL Petr
In reply to this post by Xumin Zeng
Hi

[hidden email] napsal dne 08.03.2010 18:55:10:

> Here is the example.
>
> > age=18:29
> > height=c(76.1,77,78.1,78.2,78.8,79.7,79.9,81.1,81.2,81.8,82.8,83.5)
> > type=c("A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D")
> > typec=c("0","4","2","9","0","7","2","3","0","1","2","3")
> > typen=c(0,1,2,3,0,1,2,3,0,1,2,3)
> > data1=data.frame(age=age,height=height, type=type, typec=typec,
> typen=typen)

With

data1=data.frame(age=age,height=height, type=type, typec=typec,
typen=typen, stringsAsFactors=F)

your conversion shall work

Regards
Petr


>
> > data1[,3]=as.numeric(data1[,3])
> > data1[,4]=as.numeric(data1[,4])
> > data1[,5]=as.numeric(data1[,5])
>
> > print(data1)
>
> and I got the output as:
>
>    age height type typec typen
> 1   18   76.1    1     1     0
> 2   19   77.0    2     5     1
> 3   20   78.1    3     3     2
> 4   21   78.2    4     7     3
> 5   22   78.8    1     1     0
> 6   23   79.7    2     6     1
> 7   24   79.9    3     3     2
> 8   25   81.1    4     4     3
> 9   26   81.2    1     1     0
> 10  27   81.8    2     2     1
> 11  28   82.8    3     3     2
> 12  29   83.5    4     4     3
>
> The "typec" is not what I expected. How can I get the direct conversion
> from character to numeric and get the following output?
>
>    age height type typec typen
> 1   18   76.1    1     0     0
> 2   19   77.0    2     4     1
> 3   20   78.1    3     2     2
> 4   21   78.2    4     9     3
> 5   22   78.8    1     0     0
> 6   23   79.7    2     7     1
> 7   24   79.9    3     2     2
> 8   25   81.1    4     3     3
> 9   26   81.2    1     0     0
> 10  27   81.8    2     1     1
> 11  28   82.8    3     2     2
> 12  29   83.5    4     3     3
>
> Thanks.
>
> Xumin
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Odp: how to convert character variables into numeric variables directly

Xumin Zeng
Thanks!  Xumin




Petr PIKAL <[hidden email]>
03/09/2010 01:57 AM

To
Xumin Zeng <[hidden email]>
cc
r-help <[hidden email]>
Subject
Odp: [R] how to convert character variables into numeric variables
directly






Hi

[hidden email] napsal dne 08.03.2010 18:55:10:

> Here is the example.
>
> > age=18:29
> > height=c(76.1,77,78.1,78.2,78.8,79.7,79.9,81.1,81.2,81.8,82.8,83.5)
> > type=c("A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D")
> > typec=c("0","4","2","9","0","7","2","3","0","1","2","3")
> > typen=c(0,1,2,3,0,1,2,3,0,1,2,3)
> > data1=data.frame(age=age,height=height, type=type, typec=typec,
> typen=typen)

With

data1=data.frame(age=age,height=height, type=type, typec=typec,
typen=typen, stringsAsFactors=F)

your conversion shall work

Regards
Petr


>
> > data1[,3]=as.numeric(data1[,3])
> > data1[,4]=as.numeric(data1[,4])
> > data1[,5]=as.numeric(data1[,5])
>
> > print(data1)
>
> and I got the output as:
>
>    age height type typec typen
> 1   18   76.1    1     1     0
> 2   19   77.0    2     5     1
> 3   20   78.1    3     3     2
> 4   21   78.2    4     7     3
> 5   22   78.8    1     1     0
> 6   23   79.7    2     6     1
> 7   24   79.9    3     3     2
> 8   25   81.1    4     4     3
> 9   26   81.2    1     1     0
> 10  27   81.8    2     2     1
> 11  28   82.8    3     3     2
> 12  29   83.5    4     4     3
>
> The "typec" is not what I expected. How can I get the direct conversion
> from character to numeric and get the following output?
>
>    age height type typec typen
> 1   18   76.1    1     0     0
> 2   19   77.0    2     4     1
> 3   20   78.1    3     2     2
> 4   21   78.2    4     9     3
> 5   22   78.8    1     0     0
> 6   23   79.7    2     7     1
> 7   24   79.9    3     2     2
> 8   25   81.1    4     3     3
> 9   26   81.2    1     0     0
> 10  27   81.8    2     1     1
> 11  28   82.8    3     2     2
> 12  29   83.5    4     3     3
>
> Thanks.
>
> Xumin
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Loading...