Select the numeric columns and one column specified by name from the data frame

I have a data frame that contains both numeric and non-numeric columns, say

df <- data.frame(v1=1:20,v2=1:20,v3=1:20,v4=letters[1:20],v5=letters[1:20]) 

To select only non-numeric columns, I would use

 fixCol <- !sapply(df,is.numeric) 

But now I also want to include a specific numeric column, say v2. My data frame is very large and the order of the columns is changing, so I cannot index it with a number, I really want to use the name "v2". I tried

 fixCol$v2 = TRUE 

but this gives me a warning In fixCol$FR = TRUE : Coercing LHS to a list , which makes it impossible for a subset of my original data frame to get only fixCol

 df[,fixCol] 

gives: Error in .subset(x, j) : invalid subscript type 'list'

In the end, my goal is to scale all the numeric columns of my data frame except this specified column using something like this

 scaleCol = !fixCol df_scaled = cbind(df[,fixCol], sapply(df[,scaleCol],scale)) 

How can i do this?

+5
source share
2 answers

We can use the OR condition ( | ) to get a logical index, and then a subset of the 'df' columns.

 df1 <- df[!sapply(df, is.numeric)|names(df)=='v2'] head(df1,2) # v2 v4 v5 #1 1 aa #2 2 bb 
+3
source
 fixCol <- !sapply(df,is.numeric) fixCol <- df[, fixCol] fixCol$v2 <- df[colnames(df)=="v2"] head(fixCol) # v4 v5 v2 #1 aa 1 #2 bb 2 #3 cc 3 #4 dd 4 #5 ee 5 #6 ff 6 
+2
source

Source: https://habr.com/ru/post/1242851/


All Articles