对列表中的data.frames执行逻辑运算

时间:2016-08-19 13:24:38

标签: r list dataframe logical-operators

我有一个维度为10,2的二维列表对象。 每个元素都有一个data.frame,包含8100个带有2个变量的观察值。

我正在尝试获取一个相同形状的对象,其中包含我在data.frames的每个元素上运行的测试的逻辑。

到目前为止,我尝试了下面的代码,但它为每个data.frame提供了逻辑,而不是这些中的元素

# sample data: http://www.filedropper.com/sample_1
# load('sample.Rdata')

str(temp)

List of 20
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -223 -224 -203 -198 -214 ...
..$ Var2: num [1:8100] -178 -178 -178 -178 -178 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -223 -224 -203 -198 -214 ...
..$ Var2: num [1:8100] -178 -178 -178 -178 -178 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -223 -224 -203 -197 -214 ...
..$ Var2: num [1:8100] -178 -178 -178 -178 -178 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -223 -223 -203 -197 -214 ...
..$ Var2: num [1:8100] -177 -177 -177 -177 -177 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -222 -223 -202 -197 -213 ...
..$ Var2: num [1:8100] -177 -177 -177 -177 -177 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -222 -223 -202 -197 -213 ...
..$ Var2: num [1:8100] -177 -177 -177 -177 -177 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -222 -223 -202 -196 -213 ...
..$ Var2: num [1:8100] -177 -177 -177 -177 -177 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -222 -222 -202 -196 -213 ...
..$ Var2: num [1:8100] -176 -176 -176 -176 -176 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -221 -222 -201 -196 -212 ...
..$ Var2: num [1:8100] -176 -176 -176 -176 -176 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -221 -222 -201 -196 -212 ...
..$ Var2: num [1:8100] -176 -176 -176 -176 -176 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -133 -134 -113 -108 -124 ...
..$ Var2: num [1:8100] -88.2 -88.2 -88.2 -88.2 -88.2 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -133 -134 -113 -108 -124 ...
..$ Var2: num [1:8100] -88.2 -88.2 -88.2 -88.2 -88.2 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -133 -134 -113 -108 -124 ...
..$ Var2: num [1:8100] -88.2 -88.2 -88.2 -88.2 -88.2 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -133 -134 -113 -108 -124 ...
..$ Var2: num [1:8100] -88.2 -88.2 -88.2 -88.2 -88.2 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -133 -134 -113 -108 -124 ...
..$ Var2: num [1:8100] -88.2 -88.2 -88.2 -88.2 -88.2 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -133 -134 -113 -108 -124 ...
..$ Var2: num [1:8100] -88.2 -88.2 -88.2 -88.2 -88.2 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -133 -134 -113 -108 -124 ...
..$ Var2: num [1:8100] -88.2 -88.2 -88.2 -88.2 -88.2 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -133 -134 -113 -108 -124 ...
..$ Var2: num [1:8100] -88.2 -88.2 -88.2 -88.2 -88.2 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -133 -134 -113 -108 -124 ...
..$ Var2: num [1:8100] -88.2 -88.2 -88.2 -88.2 -88.2 ...
  $ :'data.frame':  8100 obs. of  2 variables:
..$ Var1: num [1:8100] -133 -134 -113 -108 -124 ...
..$ Var2: num [1:8100] -88.2 -88.2 -88.2 -88.2 -88.2 ...
- attr(*, "dim")= int [1:2] 10 2
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:2] "Var1" "Var2"


test <- function(x) {  
  x > -50 & x < -70
}

out = sapply(seq_along(temp), function(x) test(x))

1 个答案:

答案 0 :(得分:0)

我们需要根据索引获取'temp',然后应用test。它将返回list逻辑矩阵。基于OP对象中的描述,即相同对象意味着具有相同的维度(?)。在这种情况下,请尝试

lapply(seq_along(temp), function(i) test(temp[[i]]))