Thank you Jim, but I obtain: ```> str(df)'data.frame': 5 obs. of 3 variables: $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 $ sales : num 13 16 22 27 34 $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5> df1<-df[,!unlist(lapply(df,is.factor))] > str(df1)num [1:5] 13 16 22 27 34> df1[1] 13 16 22 27 34 ``` I was expecting ``` str(df) 'data.frame': 5 obs. of 3 variables: $ region : char "A","B","C","D",..: 1 2 3 4 5 $ sales : num 13 16 22 27 34 $ country: char "a","b","c","d",..: 1 2 3 4 5 ``` On Sun, Sep 19, 2021 at 11:37 AM Jim Lemon <drjimlemon at gmail.com> wrote:> > Hi Luigi, > It's easy: > > df1<-df[,!unlist(lapply(df,is.factor))] > > _except_ when there is only one column left, as in your example. In > that case, you will have to coerce the resulting vector back into a > one column data frame. > > Jim > > On Sun, Sep 19, 2021 at 6:18 PM Luigi Marongiu <marongiu.luigi at gmail.com> wrote: > > > > Hello, > > I woul dlike to remove factors from all the columns of a dataframe. > > I can do it n a column at the time with > > ``` > > > > df <- data.frame(region=factor(c('A', 'B', 'C', 'D', 'E')), > > sales = c(13, 16, 22, 27, 34), country=factor(c('a', > > 'b', 'c', 'd', 'e'))) > > > > new_df$region <- droplevels(new_df$region) > > ``` > > > > What is the syntax to remove all factors at once (from all columns)? > > For this does not work: > > ``` > > > str(df) > > 'data.frame': 5 obs. of 3 variables: > > $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 > > $ sales : num 13 16 22 27 34 > > $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 > > > df = droplevels(df) > > > str(df) > > 'data.frame': 5 obs. of 3 variables: > > $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 > > $ sales : num 13 16 22 27 34 > > $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 > > ``` > > Thank you > > > > ______________________________________________ > > R-help at 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.-- Best regards, Luigi
Hi Luigi, try this library(taRifx) df_noFact <- remove.factors(df) str(df_noFact) 'data.frame': 5 obs. of 3 variables: $ region : chr "A" "B" "C" "D" ... $ sales : num 13 16 22 27 34 $ country: chr "a" "b" "c" "d" ... I hope this helps Fabio Il giorno dom 19 set 2021 alle ore 12:04 Luigi Marongiu < marongiu.luigi at gmail.com> ha scritto:> Thank you Jim, but I obtain: > ``` > > str(df) > 'data.frame': 5 obs. of 3 variables: > $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 > $ sales : num 13 16 22 27 34 > $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 > > df1<-df[,!unlist(lapply(df,is.factor))] > > str(df1) > num [1:5] 13 16 22 27 34 > > df1 > [1] 13 16 22 27 34 > ``` > I was expecting > ``` > str(df) > 'data.frame': 5 obs. of 3 variables: > $ region : char "A","B","C","D",..: 1 2 3 4 5 > $ sales : num 13 16 22 27 34 > $ country: char "a","b","c","d",..: 1 2 3 4 5 > ``` > > On Sun, Sep 19, 2021 at 11:37 AM Jim Lemon <drjimlemon at gmail.com> wrote: > > > > Hi Luigi, > > It's easy: > > > > df1<-df[,!unlist(lapply(df,is.factor))] > > > > _except_ when there is only one column left, as in your example. In > > that case, you will have to coerce the resulting vector back into a > > one column data frame. > > > > Jim > > > > On Sun, Sep 19, 2021 at 6:18 PM Luigi Marongiu <marongiu.luigi at gmail.com> > wrote: > > > > > > Hello, > > > I woul dlike to remove factors from all the columns of a dataframe. > > > I can do it n a column at the time with > > > ``` > > > > > > df <- data.frame(region=factor(c('A', 'B', 'C', 'D', 'E')), > > > sales = c(13, 16, 22, 27, 34), country=factor(c('a', > > > 'b', 'c', 'd', 'e'))) > > > > > > new_df$region <- droplevels(new_df$region) > > > ``` > > > > > > What is the syntax to remove all factors at once (from all columns)? > > > For this does not work: > > > ``` > > > > str(df) > > > 'data.frame': 5 obs. of 3 variables: > > > $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 > > > $ sales : num 13 16 22 27 34 > > > $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 > > > > df = droplevels(df) > > > > str(df) > > > 'data.frame': 5 obs. of 3 variables: > > > $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 > > > $ sales : num 13 16 22 27 34 > > > $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 > > > ``` > > > Thank you > > > > > > ______________________________________________ > > > R-help at 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. > > > > -- > Best regards, > Luigi > > ______________________________________________ > R-help at 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]]
Hi Luigi, Okay, so you mean: I want to change all factors in a dataframe to character class, _not_ remove them. factor2character<-function(x) if(is.factor(x)) return(as.character(x)) else return(x) df1<-lapply(df,factor2character) Jim On Sun, Sep 19, 2021 at 8:03 PM Luigi Marongiu <marongiu.luigi at gmail.com> wrote:> > Thank you Jim, but I obtain: > ``` > > str(df) > 'data.frame': 5 obs. of 3 variables: > $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 > $ sales : num 13 16 22 27 34 > $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 > > df1<-df[,!unlist(lapply(df,is.factor))] > > str(df1) > num [1:5] 13 16 22 27 34 > > df1 > [1] 13 16 22 27 34 > ``` > I was expecting > ``` > str(df) > 'data.frame': 5 obs. of 3 variables: > $ region : char "A","B","C","D",..: 1 2 3 4 5 > $ sales : num 13 16 22 27 34 > $ country: char "a","b","c","d",..: 1 2 3 4 5 > ``` > > On Sun, Sep 19, 2021 at 11:37 AM Jim Lemon <drjimlemon at gmail.com> wrote: > > > > Hi Luigi, > > It's easy: > > > > df1<-df[,!unlist(lapply(df,is.factor))] > > > > _except_ when there is only one column left, as in your example. In > > that case, you will have to coerce the resulting vector back into a > > one column data frame. > > > > Jim > > > > On Sun, Sep 19, 2021 at 6:18 PM Luigi Marongiu <marongiu.luigi at gmail.com> wrote: > > > > > > Hello, > > > I woul dlike to remove factors from all the columns of a dataframe. > > > I can do it n a column at the time with > > > ``` > > > > > > df <- data.frame(region=factor(c('A', 'B', 'C', 'D', 'E')), > > > sales = c(13, 16, 22, 27, 34), country=factor(c('a', > > > 'b', 'c', 'd', 'e'))) > > > > > > new_df$region <- droplevels(new_df$region) > > > ``` > > > > > > What is the syntax to remove all factors at once (from all columns)? > > > For this does not work: > > > ``` > > > > str(df) > > > 'data.frame': 5 obs. of 3 variables: > > > $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 > > > $ sales : num 13 16 22 27 34 > > > $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 > > > > df = droplevels(df) > > > > str(df) > > > 'data.frame': 5 obs. of 3 variables: > > > $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 > > > $ sales : num 13 16 22 27 34 > > > $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 > > > ``` > > > Thank you > > > > > > ______________________________________________ > > > R-help at 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. > > > > -- > Best regards, > Luigi
Hello, Using Jim's lapply(., is.factor) but simplified, you could do df1 <- df i <- sapply(df1, is.factor) df1[i] <- lapply(df1[i], as.character) a one-liner modifying df, not df1 is df[sapply(df, is.factor)] <- lapply(df[sapply(df, is.factor)], as.character) Hope this helps, Rui Barradas ?s 11:03 de 19/09/21, Luigi Marongiu escreveu:> Thank you Jim, but I obtain: > ``` >> str(df) > 'data.frame': 5 obs. of 3 variables: > $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 > $ sales : num 13 16 22 27 34 > $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 >> df1<-df[,!unlist(lapply(df,is.factor))] >> str(df1) > num [1:5] 13 16 22 27 34 >> df1 > [1] 13 16 22 27 34 > ``` > I was expecting > ``` > str(df) > 'data.frame': 5 obs. of 3 variables: > $ region : char "A","B","C","D",..: 1 2 3 4 5 > $ sales : num 13 16 22 27 34 > $ country: char "a","b","c","d",..: 1 2 3 4 5 > ``` > > On Sun, Sep 19, 2021 at 11:37 AM Jim Lemon <drjimlemon at gmail.com> wrote: >> >> Hi Luigi, >> It's easy: >> >> df1<-df[,!unlist(lapply(df,is.factor))] >> >> _except_ when there is only one column left, as in your example. In >> that case, you will have to coerce the resulting vector back into a >> one column data frame. >> >> Jim >> >> On Sun, Sep 19, 2021 at 6:18 PM Luigi Marongiu <marongiu.luigi at gmail.com> wrote: >>> >>> Hello, >>> I woul dlike to remove factors from all the columns of a dataframe. >>> I can do it n a column at the time with >>> ``` >>> >>> df <- data.frame(region=factor(c('A', 'B', 'C', 'D', 'E')), >>> sales = c(13, 16, 22, 27, 34), country=factor(c('a', >>> 'b', 'c', 'd', 'e'))) >>> >>> new_df$region <- droplevels(new_df$region) >>> ``` >>> >>> What is the syntax to remove all factors at once (from all columns)? >>> For this does not work: >>> ``` >>>> str(df) >>> 'data.frame': 5 obs. of 3 variables: >>> $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 >>> $ sales : num 13 16 22 27 34 >>> $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 >>>> df = droplevels(df) >>>> str(df) >>> 'data.frame': 5 obs. of 3 variables: >>> $ region : Factor w/ 5 levels "A","B","C","D",..: 1 2 3 4 5 >>> $ sales : num 13 16 22 27 34 >>> $ country: Factor w/ 5 levels "a","b","c","d",..: 1 2 3 4 5 >>> ``` >>> Thank you >>> >>> ______________________________________________ >>> R-help at 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. > > >