zhihua li
2005-Dec-08 09:29 UTC
[R] how to change a dataframe with characters to a numeric matrix?
hi netters, i have a dataframe TEST like this: Y1 Y2 Y3 X1 4 7 8 X2 6 2 Z X3 8 0 1 i would like to change it to a numeric matrix, replacing "Z" with NA Y1 Y2 Y3 X1 4 7 8 X2 6 2 NA X3 8 0 1 i've tried the function data.matrix but it didn't work. is there any easy way to do this? thanks a lot!
David Hajage
2005-Dec-08 09:51 UTC
[R] how to change a dataframe with characters to a numeric matrix?
A way to do it : TEST$Y3[TEST$Y3=="Z"] <- NA TEST$Y3 <- as.numeric(TEST$Y3) 2005/12/8, zhihua li <lzhtom@hotmail.com>:> > hi netters, > > i have a dataframe TEST like this: > > Y1 Y2 Y3 > X1 4 7 8 > X2 6 2 Z > X3 8 0 1 > > i would like to change it to a numeric matrix, replacing "Z" with NA > > Y1 Y2 Y3 > X1 4 7 8 > X2 6 2 NA > X3 8 0 1 > > i've tried the function data.matrix but it didn't work. is there any easy > way to do this? > > thanks a lot! > > > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >-- David [[alternative HTML version deleted]]
Jacques VESLOT
2005-Dec-08 10:02 UTC
[R] how to change a dataframe with characters to a numeric matrix?
you probably have a dataframe like this : z <- data.frame(y1=c(1,2,3),y2=c(4,5,6),y3=c(4,"z",5)) you can do : z <- as.matrix(z) mode(z) <- "numeric" zhihua li a 仺仸crit :> hi netters, > > i have a dataframe TEST like this: > > Y1 Y2 Y3 > X1 4 7 8 > X2 6 2 Z > X3 8 0 1 > > i would like to change it to a numeric matrix, replacing "Z" with NA > > Y1 Y2 Y3 > X1 4 7 8 > X2 6 2 NA > X3 8 0 1 > > i've tried the function data.matrix but it didn't work. is there any > easy way to do this? > > thanks a lot! > >------------------------------------------------------------------------ > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Reasonably Related Threads
- An R clause to bind dataframes under certain contions
- locate the rows in a dataframe with some criteria
- Error: evaluation nested too deeply when doing heatmap with binary distfunction
- manipulating dataframe according to the values of some columns
- Comparison of aggregate in R and group by in mysql