r - select column in data frame based on NAs in rows -


say have data frame of 7 columns, rows having 7 values , others nas past point. want grab last value (going left right) not na , value directly left. hierarchical data, groups go deeper others. want deepest , second deepest groups in 2 columns in new data frame.

this code works maxes out memory data frame of 46k observations. there more efficient way i'm not thinking of?

df <- data.frame(level1 = c('animal', 'vegetable', 'mineral'),                  level2 = c('mammal', 'pepper', 'rock'),                  level3 = c('dog', 'jalepeno', na),                  level4 = c('westie', na, na))  deepest <- apply(df, 1,                    function(x) length(which(!is.na(x)))) one.up <- apply(df, 1,                      function(x) length(which(!is.na(x)))-1) len <- nrow(df) output <- data.frame(one.up = unlist(sapply(1:len,                              function(x) df[x, one.up[x]])),                      deepest= unlist(sapply(1:len,                                              function(x) df[x, deepest[x]]))) 

first time posting. can cobble need site. in advance.

i think save running loop twice simple apply call, like:

> apply(df, 1, function(x) { +     n <- max(which(!is.na(x))) +     x[(n-1):n] + })      [,1]     [,2]       [,3]      [1,] "dog"    "pepper"   "mineral" [2,] "westie" "jalepeno" "rock"    

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -