2016-08-04 101 views
0

列表有隻有兩列在列表中選擇多個dataframes列

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 

謝謝

+4

'lapply(test_data,'[',2)' – Sotos

+0

非常感謝@Sotos。你會解釋這個解決方案嗎? –

+0

試試'test_data [[1]] [1]'或'test_data [[2]] [1]'等等,你就會得到它。提示:看看'['用於索引嗎? – Sotos

回答

0

purrr::map(lapply的增強版)提供了快捷方式,用於這種類型的操作:

# 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) 

正如可以在例如上面看到,可選擇的列在data.frame中,通過名稱,將字符串作爲第二個參數傳遞給purrr::map,或者通過傳遞一個數字按位置傳遞。