选择列序列并创建变量

时间:2015-03-17 14:07:20

标签: r dataframe seq

我想知道是否有办法通过序列选择特定列并从中创建新变量。

例如,如果我有8个n个观察列,我怎样才能创建4个顺序选择2行的变量?我的数据集比这大得多,我有1416个变量,每个变量有62个观察值(我已粘贴到下面的电子表格的链接,其中第一列和行代表名称)。我想从这个名为1-12的站点创建新的数据帧。所以网站1 = df [,1:117]; site 2 = df [,119:237]等。

我打算将此代码用于具有更多变量的未来数据集,因此如果任何人都可以阐明如何实现这一点,某些形式的循环或序列函数会非常有效吗?

https://www.dropbox.com/s/p1a5cu567lxntmw/MyData.csv?dl=0

提前谢谢你。

詹姆斯

p.s @nrussell我已经复制并粘贴了你下面提到的代码的输出,它后面跟着一系列显示的数字。

  

dput(z [,1:10])   structure(1 = c(0,0,0,0,0,0,0,0,0311410340342049,   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,   0,0,0.0207444023791158,0,0,0,0,0,0,0,0,0,0,0,0312971643732546,   0,0,0,0,0,0,0,0,0,0376287494579976,0,0,0,0,0,   0,0),......... 10 = c(0,0,0,0,1.119280313679916,   0,0,0.301029995663981,0,0,0,0,0,0,0,0,0,0.1515681882079494,   0.136831816210901,0,0,0,0.0273663632421801,0,0,0,0.0547327264843602,   0,0,0,0,0.0231561535126139,0,0,0.0903089986991944,0,   0,0.0752574989159953,0.159368821233872,0.0272640716982664,   0.0177076468037636,0,0,0.120411998265592,0,0,0,0,0.0322532138211408,   0.0250858329719984,0,0,0,0.119280313679916,0,0.17722500085254,   0.225772496747986,0,0,0,0.0954242509439325,0)),. Name = c(“1”,   “2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”),class =“data.frame”,row.names = c (NA,   -62L))

1 个答案:

答案 0 :(得分:1)

我们可以split数据集('df')和'1416'列,通过创建一个gl

的分组索引来等于'118'列
 lst <- setNames(lapply(split(1:ncol(df), as.numeric(gl(ncol(df), 118,
            ncol(df)))), function(i) df[,i]), paste0('site', 1:12))

或者您可以在不使用split

的情况下创建'lst'
 lst <- setNames(lapply(seq(1, ncol(df), by = 118), 
            function(i) df[i:(i+117)]), paste0('site', 1:12))

如果我们需要在全局环境中创建12个数据集对象,list2env是一个选项(我更愿意在'lst'本身内工作)

 list2env(lst, envir=.GlobalEnv)

使用带有'8'列的小数据集('df1')

  lst1 <- setNames(lapply(split(1:ncol(df1), as.numeric(gl(ncol(df1), 
         2, ncol(df1)))), function(i) df1[,i]), paste0('site', 1:4))
  list2env(lst1, envir=.GlobalEnv)

  head(site1,3)
  #  V1 V2
  #1  6 12
  #2  4  7
  #3 14 14

 head(site4,3)
 #  V7 V8
 #1 10  2
 #2  5  4
 #3  5  0

数据

set.seed(24)
df1 <- as.data.frame(matrix(sample(0:20, 8*10, replace=TRUE), ncol=8))