【问题标题】:How to determine the overall range of several variables of a data.frame, using variable indices?如何使用变量索引确定data.frame的几个变量的整体范围?
【发布时间】:2013-10-24 00:15:42
【问题描述】:

我有一个类似于以下内容的 data.frame:

data <- data.frame(
  x=c(1:10)
  , y1=c(3,5,1,8,2,4,5,0,8,2)
  , y2=c(1,8,2,1,4,5,3,0,8,2)
  , y3=c(13,88,2,1,4,5,3,0,-8,1)
)

(实际上data.frame中的变量要多得多,变量的名称也更长。)

我想确定范围

  1. 变量 x
  2. 所有其他变量

一种方法可能是:

xRange = range(data$x)

# using variable names:
yRange = range(data$y1, data$y2, data$y3)

# using variable indices
yRange = range(data[[2]], data[[3]], data[[4]])

我也尝试使用范围,这样我就不必添加每个变量名或索引:

yRange = range(data[[c(2:4)]])
yRange = range(data[[2:4]])

有没有办法从 data.frame 的多个变量中获取所有值的列表?

【问题讨论】:

    标签: arrays r list indexing dataframe


    【解决方案1】:

    不清楚你想做什么。但是为什么不使用subset 按名称删除一些变量并获取结果范围呢?

    range(subset(data,select=-x))
    -8 88
    

    或者等价于使用变量索引(不推荐)

    range(subset(data,select=2:4))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 2020-09-10
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多