在数据框列表中选择一列
Selecting a column in a list of dataframes
我有一个数据框列表,我想select使用这些数据框中的特定列。
plot_list <- lapply(plot_list, subset,
subset = format(DateAndTime, "%Y") == year)
在这行代码中,我想 select 列表 plot_list
中日期帧中的 DateAndTime 列。最有效的方法是什么?
这是一个可重现的示例,您可以根据自己的数据进行调整:
data(iris) # Measurements on three species of iris
iris.list <- split(iris, iris$species) # Create a list of data frames
SL <- lapply(iris.list, "[", "Sepal.Length") # Pull out the Sepal.Length measurements
str(SL)
# List of 3
# $ setosa :'data.frame': 50 obs. of 1 variable:
# ..$ Sepal.Length: num [1:50] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
# $ versicolor:'data.frame': 50 obs. of 1 variable:
# ..$ Sepal.Length: num [1:50] 7 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 ...
# $ virginica :'data.frame': 50 obs. of 1 variable:
# ..$ Sepal.Length: num [1:50] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 ...
result<-lapply(plot_list, function(x) x[,'DateAndTime'])
我有一个数据框列表,我想select使用这些数据框中的特定列。
plot_list <- lapply(plot_list, subset,
subset = format(DateAndTime, "%Y") == year)
在这行代码中,我想 select 列表 plot_list
中日期帧中的 DateAndTime 列。最有效的方法是什么?
这是一个可重现的示例,您可以根据自己的数据进行调整:
data(iris) # Measurements on three species of iris
iris.list <- split(iris, iris$species) # Create a list of data frames
SL <- lapply(iris.list, "[", "Sepal.Length") # Pull out the Sepal.Length measurements
str(SL)
# List of 3
# $ setosa :'data.frame': 50 obs. of 1 variable:
# ..$ Sepal.Length: num [1:50] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
# $ versicolor:'data.frame': 50 obs. of 1 variable:
# ..$ Sepal.Length: num [1:50] 7 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 ...
# $ virginica :'data.frame': 50 obs. of 1 variable:
# ..$ Sepal.Length: num [1:50] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 ...
result<-lapply(plot_list, function(x) x[,'DateAndTime'])