gpt4 book ai didi

r - 你如何根据列名对数据框进行子集化?

转载 作者:行者123 更新时间:2023-12-02 06:29:09 25 4
gpt4 key购买 nike

我有这个数据框:

 dput(df)
structure(list(Server = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "servera", class = "factor"),
Date = structure(1:6, .Label = c("7/13/2017 15:01", "7/13/2017 15:02",
"7/13/2017 15:03", "7/13/2017 15:04", "7/13/2017 15:05",
"7/13/2017 15:06"), class = "factor"), Host_CPU = c(1.812950134,
2.288070679, 1.563278198, 1.925239563, 5.350669861, 2.612503052
), UsedMemPercent = c(38.19, 38.19, 38.19, 38.19, 38.19,
38.22), jvm1 = c(10.91, 11.13, 11.34, 11.56, 11.77, 11.99
), jvm2 = c(11.47, 11.7, 11.91, 12.13, 12.35, 12.57), jvm3 = c(75.65,
76.88, 56.93, 58.99, 65.29, 67.97), jvm4 = c(39.43, 40.86,
42.27, 43.71, 45.09, 45.33), jvm5 = c(27.42, 29.63, 31.02,
32.37, 33.72, 37.71)), .Names = c("Server", "Date", "Host_CPU",
"UsedMemPercent", "jvm1", "jvm2", "jvm3", "jvm4", "jvm5"), class = "data.frame", row.names = c(NA,
-6L))

我只想能够根据此变量中的向量名称对这个数据框进行子集化:
select<-c("jvm3", "jvm4", "jvm5")

所以,我最终的 df 应该是这样的:
structure(list(Server = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "servera", class = "factor"), 
Date = structure(1:6, .Label = c("7/13/2017 15:01", "7/13/2017 15:02",
"7/13/2017 15:03", "7/13/2017 15:04", "7/13/2017 15:05",
"7/13/2017 15:06"), class = "factor"), Host_CPU = c(1.812950134,
2.288070679, 1.563278198, 1.925239563, 5.350669861, 2.612503052
), UsedMemPercent = c(38.19, 38.19, 38.19, 38.19, 38.19,
38.22), jvm3 = c(75.65, 76.88, 56.93, 58.99, 65.29, 67.97
), jvm4 = c(39.43, 40.86, 42.27, 43.71, 45.09, 45.33), jvm5 = c(27.42,
29.63, 31.02, 32.37, 33.72, 37.71)), .Names = c("Server",
"Date", "Host_CPU", "UsedMemPercent", "jvm3", "jvm4", "jvm5"), class = "data.frame", row.names = c(NA,
-6L))

有任何想法吗?

最佳答案

将数据帧保存到变量 df:

df <-
structure(
list(
Server = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "servera", class = "factor"),
Date = structure(
1:6,
.Label = c(
"7/13/2017 15:01",
"7/13/2017 15:02",
"7/13/2017 15:03",
"7/13/2017 15:04",
"7/13/2017 15:05",
"7/13/2017 15:06"
),
class = "factor"
),
Host_CPU = c(
1.812950134,
2.288070679,
1.563278198,
1.925239563,
5.350669861,
2.612503052
),
UsedMemPercent = c(38.19, 38.19, 38.19, 38.19, 38.19,
38.22),
jvm1 = c(10.91, 11.13, 11.34, 11.56, 11.77, 11.99),
jvm2 = c(11.47, 11.7, 11.91, 12.13, 12.35, 12.57),
jvm3 = c(75.65,
76.88, 56.93, 58.99, 65.29, 67.97),
jvm4 = c(39.43, 40.86,
42.27, 43.71, 45.09, 45.33),
jvm5 = c(27.42, 29.63, 31.02,
32.37, 33.72, 37.71)
),
.Names = c(
"Server",
"Date",
"Host_CPU",
"UsedMemPercent",
"jvm1",
"jvm2",
"jvm3",
"jvm4",
"jvm5"
),
class = "data.frame",
row.names = c(NA,-6L)
)
df[,select] 应该是你要找的

关于r - 你如何根据列名对数据框进行子集化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45109061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com