|
Dear All,
I'm not sure if I understand the parameter stringsAsFactors correctly. I'm trying to convert the string columns in aframe1 to factors. But it seems stringsAsFactors=T in as.data.frame() doesn't do anything. Could anybody let know what is the correct way to converting strings to factors? > aframe1=data.frame(x=LETTERS[1:10], y=LETTERS[1:10], stringsAsFactors=F) > aframe2=as.data.frame(aframe1, stringsAsFactors=T) > > str(aframe1) 'data.frame': 10 obs. of 2 variables: $ x: chr "A" "B" "C" "D" ... $ y: chr "A" "B" "C" "D" ... > str(aframe2) 'data.frame': 10 obs. of 2 variables: $ x: chr "A" "B" "C" "D" ... $ y: chr "A" "B" "C" "D" ... > Thanks, John [[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. |
|
Hi John,
Use factor(): aframe2$x <- factor(aframe2$x) Or, for all columns at once (there might be an easier solution): aframe2 <- as.data.frame(lapply(aframe1, factor)) see ?factor HTH, Ivan Le 2/28/2011 18:13, John Edwards a écrit : > Dear All, > > I'm not sure if I understand the parameter stringsAsFactors correctly. I'm > trying to convert the string columns in aframe1 to factors. But it > seems stringsAsFactors=T in as.data.frame() doesn't do anything. Could > anybody let know what is the correct way to converting strings to factors? > >> aframe1=data.frame(x=LETTERS[1:10], y=LETTERS[1:10], stringsAsFactors=F) >> aframe2=as.data.frame(aframe1, stringsAsFactors=T) >> >> str(aframe1) > 'data.frame': 10 obs. of 2 variables: > $ x: chr "A" "B" "C" "D" ... > $ y: chr "A" "B" "C" "D" ... >> str(aframe2) > 'data.frame': 10 obs. of 2 variables: > $ x: chr "A" "B" "C" "D" ... > $ y: chr "A" "B" "C" "D" ... > Thanks, > John > > [[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. > -- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 [hidden email] ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php ______________________________________________ [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. |
|
In reply to this post by John Edwards
John,
as.data.frame is a generic function that will call different methods depending on what class of object you pass to it. The different methods may have different arguments that they expect or honor. The stringsAsFactors parameter is only used in certain methods of as.data.frame. When you pass an actual data.frame to that function, stringsAsFactors is not used. It looks like it is only used for methods for character and matrix objects. So, to do what you want: aframe1 <- data.frame(x = LETTERS[1:10], y = LETTERS[1:10], z = 1:10, stringsAsFactors = FALSE) ind <- sapply(aframe1, is.character) aframe1[ind] <- lapply(aframe1[ind], factor) John Edwards wrote: > Dear All, > > I'm not sure if I understand the parameter stringsAsFactors correctly. I'm > trying to convert the string columns in aframe1 to factors. But it > seems stringsAsFactors=T in as.data.frame() doesn't do anything. Could > anybody let know what is the correct way to converting strings to factors? > >> aframe1=data.frame(x=LETTERS[1:10], y=LETTERS[1:10], stringsAsFactors=F) >> aframe2=as.data.frame(aframe1, stringsAsFactors=T) >> >> str(aframe1) > 'data.frame': 10 obs. of 2 variables: > $ x: chr "A" "B" "C" "D" ... > $ y: chr "A" "B" "C" "D" ... >> str(aframe2) > 'data.frame': 10 obs. of 2 variables: > $ x: chr "A" "B" "C" "D" ... > $ y: chr "A" "B" "C" "D" ... > > Thanks, > John > > [[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. |
| Powered by Nabble | Edit this page |
