如何根据列名称对数据框进行子集化?

时间:2017-07-14 17:55:03

标签: r

我有这个数据框:

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

任何想法?

3 个答案:

答案 0 :(得分:5)

请重新访问指数。如果在R中使用索引机制[,则可以主要使用三种类型的索引:

  • 逻辑向量:与列数相同的长度,TRUE表示选择列
  • 数字向量:根据位置选择列
  • 字符向量:根据名称选择列

如果对数据框使用索引机制,则可以通过两种方式处理这些对象:

  • 作为列表,因为它们是内部列表
  • 作为矩阵,因为它们在许多情况下模仿矩阵行为

iris数据框为例,比较从数据框中选择列的多种方法。如果将其视为列表,则有以下两个选项:

如果您想要一个向量形式的单个列,请使用[[

iris[["Species"]]
# [1] setosa     setosa     setosa ... : is a vector

如果您想要一个或多个列,请使用[,但需要返回数据框:

iris["Species"]
iris[c("Sepal.Width", "Species")]

如果将其视为矩阵,则只需执行与矩阵相同的操作即可。如果未指定任何行索引,则这些命令实际上与上面使用的命令相同:

iris[ , "Species"] # is the same as iris[["Species"]]
iris[ , "Species", drop = FALSE] # is the same as iris["Species"]
iris[ , c("Sepal.Width", "Species")] # is the same as iris[c("Sepal.Width", "Species")]

所以在你的情况下,你只需要:

select <- c("Server","Date","Host_CPU","UsedMemPercent",
            "jvm3","jvm4","jvm5")
df[select]

有关子集的注意事项:子集有效,但只能以交互方式使用。帮助页面上有一条警告说明:

  

这是一种便于交互使用的便利功能。对于   编程最好使用标准的子集函数   [,特别是论证子集的非标准评估   可能会产生意想不到的后果。

答案 1 :(得分:2)

将数据框保存到变量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]应该是你要找的东西

答案 2 :(得分:1)

以这种方式:

df[,c(1:4,7:9)]

您还可以使用dplyr选择列:

select(df, Server,Date,Host_CPU,UsedMemPercent,jvm3,jvm4,jvm5)