Search code examples
rvariablesempty-list

Creating a complicated empty list or data frame


I have a variable that is a list.

[[1]]

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]

 [1,]    1    1    1    1    1    1    1    4

 [2,]    1    1    1    1    1    1    1    4

 [3,]    1    1    1    1    1    1    1    4

 [4,]    1    1    1    1    1    1    1    4

 [5,]    1    1    1    1    1    1    1    4

 [6,]    1    1    1    1    1    1    1    4

 [7,]    1    1    1    1    1    1    1    4

 [8,]    1    1    1    1    1    1    1    4

 [9,]    1    1    1    1    1    1    1    4

[10,]    1    1    1    1    1    1    1    4

[11,]    1    1    1    1    1    1    1    4



[[2]]

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]

 [1,]    1    1    1    1    1    1    1    3

 [2,]    1    1    1    1    1    1    1    4

 [3,]    1    1    1    1    1    1    1    4

 [4,]    1    1    1    1    1    1    1    3

 [5,]    1    1    1    1    1    1    1    4

 [6,]    1    1    1    1    1    1    1    4

 [7,]    1    1    1    1    1    1    1    4

 [8,]    1    1    1    1    1    1    1    4

 [9,]    1    1    1    1    1    1    1    4

[10,]    1    1    1    1    1    1    1    4

[11,]    1    1    1    1    1    1    1    4

I need an empty variable which is same format as this variable. My function will do some computation and will be put in the same location as this variable. However in the new variable I will not have 8th column.


Solution

  • Replicate your data

    df = list(matrix(rep(1, 88), ncol = 8), matrix(rep(1, 88), ncol = 8))
    

    Remove the 8th column from all sublist

    new_df = lapply(df, function(x) x[,-8])
    

    Remove the 8th column from all sublist and replace all the 1's with NA

    new_df = lapply(df, function(x) replace(x[,-8], x[,-8] == 1, NA))