[R] how to convert 450 columns in a dataframe from numberic to character, but leave other columns unchanged?

Uwe Ligges ligges at statistik.uni-dortmund.de
Sat Apr 22 12:45:26 CEST 2006


zhongmiao wang wrote:

> Hello,
> I can do it seperately, but there must be a method to convert the 450
> columns together?

Yes.

> By the way, Can R write a seriers of variable names in a short way,
> such as X1-X450?

paste("X", 1:450, sep="")

> I can extract the 450 columns out and convert them to a character
> matrix, however, when I combined it back, they turned back to numberic

Should not do so, please specify reproducible code that shows the problem.


> automatically. I can also use I() function to preserve them as
> character, however, the column names cannot be used to create formular
> for further calculation. How to resolve this problem?

For your data.frame dat:

cn <- paste("X", 1:450, sep="")
dat[,cn] <- sapply(dat[,cn], as.character)

Uwe Ligges



> Thank you so much!
> Zhongmiao wang
> 
> ______________________________________________
> 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




More information about the R-help mailing list