Convert the last column of several dataframes to a factor in R

Viewed 31

I have three dataframes in R, df1, df2, df3 that contain different data, however, the last column contains my target variable for each dataset. However, in each instance, the last column is an integer and I want it to be a factor. I know I can do the following code to convert the column into a factor:

df1[,'lascoldf1'] <-factor(df1[,'lastcoldf1'])
df2[,'lascoldf2'] <-factor(df2[,'lastcoldf2'])
df3[,'lascoldf3'] <-factor(df3[,'lastcoldf3'])

However, this seems a little inefficient. Also, the last colname isn't always the same name, and each dataset has a different number of columns, so I just need a way of referencing the last column. I've found this way so would probably need something like the following pseudo-code:

for (df in c(df1,df2,df3)){
index = df[,ncol(df)]
lapply(df[,index] , factor)
}

However, I can't seem to get it to work properly.

1 Answers

Put the data frames into a list and in an lapply use ncol to identify the last column.

L <- lapply(list(df1, df2, df3), function(x) {x[,ncol(x)] <- as.factor(x[,ncol(x)]);x})
str(L[[1]])
# 'data.frame': 3 obs. of  4 variables:
#  $ X1: int  1 2 3
#  $ X2: int  4 5 6
#  $ X3: int  7 8 9
#  $ X4: Factor w/ 3 levels "10","11","12": 1 2 3

Data:

df1 <- df2 <- df3 <- data.frame(matrix(1:12, 3, 4))
Related