user255715 user255715 - 3 months ago 10
R Question

performing logical operation on data.frames within list

I have a 2-dimensional list object with dimension 10,2.
Each elment holds a data.frame with 8100 observations with 2 variables.

I'm trying to obtain a identical shaped object containing logicals for a test I run on each element of the data.frames.

So far I tried below code but it gives me a logical for each data.frame, not for the elements within these

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

Answer

We need to get the 'temp' based on the index and then apply the test. It will return a list of logical matrix. Based on the description in the OP's object, i.e. identical object implies that having the same dimension(?). In that case, try

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