Re: [R] Remove all factor levels from an R dataframe
Hi John, I was thinking that you created df1 in a way that set the 'year' column as a factor when this is not what you wanted to do. The data.frame() function takes an argument stringsAsFactors which controls this behavior. For R versions 3.6.3 or earlier, the default setting is stringsAsFactors=TRUE, which means that string columns automatically become factors. You have to specify stringsAsFactors=FALSE to avoid this. (In R 4.0.x the default was changed to FALSE.) Example: df1 <- data.frame( a=letters[1:10], stringsAsFactors=FALSE ) HTH, Eric On Tue, Nov 10, 2020 at 11:16 AM Jim Lemon wrote: > > Sure John, > > df1<-df1[order(as.character(df1$year),decreasing=TRUE),] > > Jim > > On Tue, Nov 10, 2020 at 8:05 PM John wrote: > > > Thanks Jim. Can we do descending order? > > > > Jim Lemon 於 2020年11月10日 週二 下午4:56寫道: > > > >> Hi John, > >> > >> df1<-sapply(df1,as.character) > >> > >> Should do what you ask. The error message probably means that you should > >> do this: > >> > >> df1<-df1[order(as.character(df1$year)),] > >> > >> as "year" is the name of the first column in df1, not a separate object. > >> > >> Jim > >> > >> On Tue, Nov 10, 2020 at 6:57 PM John wrote: > >> > >>> Hi, > >>> > >>>I would like to sort the following simple dataframe by "year" > >>> (characters), but the factor structure prevents me from doing so. How > >>> can I > >>> remove the factor structure? Thanks! > >>> > >>> > df1 > >>> year country > >>> 4 2007 Asia; survey > >>> 5 2010 8 countries in E/SE Asia > >>> 6 2015Ghana > >>> 7 > >>> 8 2000 US? > >>> > str(df1) > >>> 'data.frame': 5 obs. of 2 variables: > >>> $ year : Factor w/ 9 levels "2017","2016",..: 4 5 3 6 7 > >>> $ country: Factor w/ 9 levels "Euro Area\\newline Testing the MP > >>> performance of the Euro Area",..: 4 5 6 7 8 > >>> > df1[order(-year), ] > >>> Error in order(-year) : object 'year' not found > >>> > >>> [[alternative HTML version deleted]] > >>> > >>> __ > >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > >>> 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]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Remove all factor levels from an R dataframe
Sure John, df1<-df1[order(as.character(df1$year),decreasing=TRUE),] Jim On Tue, Nov 10, 2020 at 8:05 PM John wrote: > Thanks Jim. Can we do descending order? > > Jim Lemon 於 2020年11月10日 週二 下午4:56寫道: > >> Hi John, >> >> df1<-sapply(df1,as.character) >> >> Should do what you ask. The error message probably means that you should >> do this: >> >> df1<-df1[order(as.character(df1$year)),] >> >> as "year" is the name of the first column in df1, not a separate object. >> >> Jim >> >> On Tue, Nov 10, 2020 at 6:57 PM John wrote: >> >>> Hi, >>> >>>I would like to sort the following simple dataframe by "year" >>> (characters), but the factor structure prevents me from doing so. How >>> can I >>> remove the factor structure? Thanks! >>> >>> > df1 >>> year country >>> 4 2007 Asia; survey >>> 5 2010 8 countries in E/SE Asia >>> 6 2015Ghana >>> 7 >>> 8 2000 US? >>> > str(df1) >>> 'data.frame': 5 obs. of 2 variables: >>> $ year : Factor w/ 9 levels "2017","2016",..: 4 5 3 6 7 >>> $ country: Factor w/ 9 levels "Euro Area\\newline Testing the MP >>> performance of the Euro Area",..: 4 5 6 7 8 >>> > df1[order(-year), ] >>> Error in order(-year) : object 'year' not found >>> >>> [[alternative HTML version deleted]] >>> >>> __ >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Remove all factor levels from an R dataframe
Thanks Jim. Can we do descending order? Jim Lemon 於 2020年11月10日 週二 下午4:56寫道: > Hi John, > > df1<-sapply(df1,as.character) > > Should do what you ask. The error message probably means that you should > do this: > > df1<-df1[order(as.character(df1$year)),] > > as "year" is the name of the first column in df1, not a separate object. > > Jim > > On Tue, Nov 10, 2020 at 6:57 PM John wrote: > >> Hi, >> >>I would like to sort the following simple dataframe by "year" >> (characters), but the factor structure prevents me from doing so. How can >> I >> remove the factor structure? Thanks! >> >> > df1 >> year country >> 4 2007 Asia; survey >> 5 2010 8 countries in E/SE Asia >> 6 2015Ghana >> 7 >> 8 2000 US? >> > str(df1) >> 'data.frame': 5 obs. of 2 variables: >> $ year : Factor w/ 9 levels "2017","2016",..: 4 5 3 6 7 >> $ country: Factor w/ 9 levels "Euro Area\\newline Testing the MP >> performance of the Euro Area",..: 4 5 6 7 8 >> > df1[order(-year), ] >> Error in order(-year) : object 'year' not found >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] Remove all factor levels from an R dataframe
Hi John, df1<-sapply(df1,as.character) Should do what you ask. The error message probably means that you should do this: df1<-df1[order(as.character(df1$year)),] as "year" is the name of the first column in df1, not a separate object. Jim On Tue, Nov 10, 2020 at 6:57 PM John wrote: > Hi, > >I would like to sort the following simple dataframe by "year" > (characters), but the factor structure prevents me from doing so. How can I > remove the factor structure? Thanks! > > > df1 > year country > 4 2007 Asia; survey > 5 2010 8 countries in E/SE Asia > 6 2015Ghana > 7 > 8 2000 US? > > str(df1) > 'data.frame': 5 obs. of 2 variables: > $ year : Factor w/ 9 levels "2017","2016",..: 4 5 3 6 7 > $ country: Factor w/ 9 levels "Euro Area\\newline Testing the MP > performance of the Euro Area",..: 4 5 6 7 8 > > df1[order(-year), ] > Error in order(-year) : object 'year' not found > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] Remove all factor levels from an R dataframe
Hi, I would like to sort the following simple dataframe by "year" (characters), but the factor structure prevents me from doing so. How can I remove the factor structure? Thanks! > df1 year country 4 2007 Asia; survey 5 2010 8 countries in E/SE Asia 6 2015Ghana 7 8 2000 US? > str(df1) 'data.frame': 5 obs. of 2 variables: $ year : Factor w/ 9 levels "2017","2016",..: 4 5 3 6 7 $ country: Factor w/ 9 levels "Euro Area\\newline Testing the MP performance of the Euro Area",..: 4 5 6 7 8 > df1[order(-year), ] Error in order(-year) : object 'year' not found [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.