0

I found this question which was helpful in subsetting a DF for numeric columns only.

Selecting only numeric columns from a data frame

However I can't figure out how to do numeric columns PLUS any other columns.

I've tried:

nums <- sapply(df, is.numeric)
df <- df[, c(nums, "charcolumn")]

and:

df <- df[,c(sapply(df, is.numeric), "Pop_Size_Group")]

both of which gave me an "undefined columns selected error"

I understand that the sapply function gives me a list of TRUE/FALSE. How can I subset my df to include all numeric columns PLUS additional columns I identify?

Community
  • 1
  • 1

1 Answers1

2

Maybe selecting names and concatenating them with "Pop_Size_Group"

 df <- df[,c(names(df)[sapply(df, is.numeric)], "Pop_Size_Group")]
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138