I know in subset()
you can pull every column starting with one and ending with another pretty easily.
df <- as.data.frame(state.x77)
subset(df, Frost > 30, select=Illiteracy:Murder)
I know with brackets you can do something like df[df$Frost > 30, 3:5]
Is there a way to recreate the subset statement with brackets that uses the column names?
You will find
df <- state.x77
df[df[, "Frost"] > 150, c("Illiteracy", "Life Exp", "Murder")]
produces
Illiteracy Life Exp Murder
Alaska 1.5 69.31 11.3
Colorado 0.7 72.06 6.8
Maine 0.7 70.39 2.7
Minnesota 0.6 72.96 2.3
Montana 0.6 70.56 5.0
Nevada 0.5 69.03 11.5
New Hampshire 0.7 71.23 3.3
North Dakota 0.8 72.78 1.4
South Dakota 0.5 72.08 1.7
Vermont 0.6 71.64 5.5
Wyoming 0.6 70.29 6.9
Incidentally, state.x77
is a matrix, not a dataframe. As a matrix you could use
df <- state.x77
df[df[, "Frost"] > 150, which(colnames(df)=="Illiteracy"):which(colnames(df)=="Murder")]
or as a dataframe
df <- as.data.frame(state.x77)
df[df[, "Frost"] > 150, which(names(df) == "Illiteracy"):which(names(df) == "Murder")]
to produce the same result, and you could write a function to make this prettier
Another approach with dplyr
library(dplyr)
df = as.data.frame(state.x77)
df %>% filter(Frost > 30) %>% select(Illiteracy:Murder) %>% head()
Illiteracy Life Exp Murder
1 1.5 69.31 11.3
2 1.9 70.66 10.1
3 0.7 72.06 6.8
4 1.1 72.48 3.1
5 0.9 70.06 6.2
6 2.0 68.54 13.9
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.