[英]Select a column from multiple dataframes in a list
我的列表有多個數據框,只有兩列
DateTime Value
30-06-2016 100
31-07-2016 200
.
.
.
我只想從列表中提取“ 值 ”列。 臨時代碼對我來說是不成功的。 我在這里做錯了什么?
actual_data <- lapply(test_data, function(df) df[,is.numeric(df)])
> actual_data[[1]]
data frame with 0 columns and 12 rows
謝謝
purrr::map
( purrr::map
的增強版)提供了此類操作的快捷方式:
# Generate test data
set.seed(35156)
test_df <- data.frame('DateTime' = rnorm(100), 'Value' = rnorm(100))
test_data <- rep(list(test_df), 100)
# Use `map` from the purrr package to subset the data.frames
purrr::map(test_data, 'Value')
purrr::map(test_data, 2)
如您在上面的示例中看到的那樣,您可以通過將名稱作為字符串的第二個參數傳遞給purrr::map
來通過名稱或通過傳遞數字來在data.frame中選擇列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.