2

I am trying to replicate the success of this solution:

remove columns with NAs from all dataframes in list

or

Remove columns from dataframe where some of values are NA

with a list of dataframes:

m1<- structure(list(vPWMETRO = c(1520L, 1520L, 1520L, 1520L, 1520L), 
               vPWPUMA00 = c(500L, 900L, 1000L, 1100L, 1200L), 
               v100 = c(96.1666666666667, 71.4615384615385, 68.6363636363636, 22.5, 64.5), 
               v101 = c(5, 15, NA, NA, NA),
               v102 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)),
          .Names = c("vPWMETRO", "vPWPUMA00", "v100", "v101", "v102"), 
          row.names = 26:30, class = "data.frame")

m2<- structure(list(vPWMETRO = c(6440L, 6440L, 6440L, NA, NA), 
               vPWPUMA00 = c(1300L,2100L, 2200L, NA, NA), 
               v100 = c(38.3921568627451, 35, 12.5, NA, NA), 
               v101 = c(NA, NA, NA, NA, NA), 
               v102 = c(38.3333333333333, 68, NA, NA, NA)), 
               .Names = c("vPWMETRO", "vPWPUMA00", "v100", "v101", "v102"), 
          row.names = c("39", "40", "41", "NA", "NA.1"), class = "data.frame")
#views structure
str(m1)
str(m2)

#creates list
snag<- list(v1520=m1, v6440=m2)
str(snag)
#attempts lapply solution
prob1<- lapply(snag, function(y) y[ ,!is.na(y)])

#2nd attempt, same result on just dataframe:
x5$v6440[ , apply(x5$v6440, 2, function(x) !(is.na(x)))]

So that columns that contain all NA's are deleted within the dataframe. Thus the result should be a list of 2 df's:

v1520: vPWPUMA00, v100, v101

v6440: vPWPUMA00, v100, v102

I see the difference in the example problem is that the dimension is 1x11 and my dimensions are 5x5. I am guessing this causes the "undefined column" error but I'm not sure.

Any assistance or advice would be most appreciated.

Regards,

Community
  • 1
  • 1
Jack Ryan
  • 2,134
  • 18
  • 26

1 Answers1

2

I don't think you were looking at quite the right questions and answers.

See Remove columns from dataframe where ALL values are NA, which appears to be what you want.

You can then modify my answer to give

lapply(snag, Filter, f = function(x){!all(is.na(x))})



$v1520
   vPWMETRO vPWPUMA00     v100 v101
26     1520       500 96.16667    5
27     1520       900 71.46154   15
28     1520      1000 68.63636   NA
29     1520      1100 22.50000   NA
30     1520      1200 64.50000   NA

$v6440
     vPWMETRO vPWPUMA00     v100     v102
39       6440      1300 38.39216 38.33333
40       6440      2100 35.00000 68.00000
41       6440      2200 12.50000       NA
NA         NA        NA       NA       NA
NA.1       NA        NA       NA       NA
Community
  • 1
  • 1
mnel
  • 113,303
  • 27
  • 265
  • 254