輸入(DF)動態子集R中的模式colname的
> df
gender age LIST_12 LIST_24 LIST_42 anxious happy nervous
1 11 12 20 18 29 31 6 28
2 35 25 26 23 9 34 13 21
3 20 8 28 27 26 26 34 29
4 24 35 10 11 18 25 26 3
5 34 8 4 3 29 33 25 35
所需的輸出(dfSubset)之後的數據幀
什麼會得到只包含列的一個子集的最佳途徑在LIST_之後結束。在這種情況下,我只想要子集:焦慮,快樂和緊張的專欄。
anxious happy nervous
1 31 6 28
2 34 13 21
3 26 34 29
4 25 26 3
5 33 25 35
相關信息
我知道,我可以爲了只子集字LIST_開頭的列名運行下面的代碼。但它不是我所期待的......
dfSubset = subset(x = df, select = grep("LIST_", names(df)))
dfSubset
重現源
df <- structure(list(gender = c(11L, 35L, 20L, 24L, 34L), age = c(12L,
25L, 8L, 35L, 8L), LIST_12 = c(20L, 26L, 28L, 10L, 4L), LIST_24 = c(18L,
23L, 27L, 11L, 3L), LIST_42 = c(29L, 9L, 26L, 18L, 29L), anxious = c(31L,
34L, 26L, 25L, 33L), happy = c(6L, 13L, 34L, 26L, 25L), nervous = c(28L,
21L, 29L, 3L, 35L)), .Names = c("gender", "age", "LIST_12", "LIST_24",
"LIST_42", "anxious", "happy", "nervous"), class = "data.frame", row.names = c(NA,
-5L))
感謝理查德它的工作原理。我認爲它可以被接受爲答案。 – S12000