[R] change all . to 0 in a data.frame

jim holtman jholtman at gmail.com
Thu Sep 6 03:39:21 CEST 2007


Here is one way.  You might want to read in the data with 'as.is=TRUE'
to prevent conversion to factors.

> x <- data.frame(a=c(1,2,3,'.',5,'.'))
> str(x)
'data.frame':   6 obs. of  1 variable:
 $ a: Factor w/ 5 levels ".","1","2","3",..: 2 3 4 1 5 1
> # replace '.' with zero; either readin with 'as.is=TRUE' or convert to character
> x$a <- as.character(x$a)
> x$a[x$a == '.'] <- '0'
> x$a <- as.numeric(x$a)
> str(x)
'data.frame':   6 obs. of  1 variable:
 $ a: num  1 2 3 0 5 0
>
>


On 9/5/07, Dieter Best <dieterbest_2000 at yahoo.com> wrote:
> Hello,
>  I read in a tab delimited text file via mydata = read.delim(myfile). The text file was originally an excel file where . was used in place of 0. Now all the columns which should be integers are factors. Any ideas how to change all the . to 0 and factors back to integer?
>  Thanks a lot in advance for any suggestions,
>  -- D
>
>
> ---------------------------------
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list