0

I'm new to r programming, i need to achieve below desire output can you please help me. dataframe:

ID      Name                      
 1      null                      
 2      list(A = 10, B = 20)      
 2      list(G = 4, U = 2)        
 3      null                      
 3      null                      
 4      list(A = 7, B = 10)

Desired Output will be,

ID      Measure                   Measure.A   Measure.B                 
 1      null                      null          null                   
 2      list(A = 10, B = 20)      10             20                    
 2      list(A = 4, B = 2)         4              2                     
 3      null                       null          null                  
 3      null                       null          null                  
 4      list(A = 7, B = 10)        7              10                   
  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Sotos Aug 23 '17 at 06:35

1 Answers1

0

It is better to have NA instead of NULL elements in a data.frame. Loop through the 'Name' column (assuming it is a list with nested list elements), replace the NULL (assuming it is real NULL and not a character string "null") with NA and rbind the elements using do.call. Assign the output to create two new columns in 'df1'

df1[c("Measure.A", "Measure.B")] <- unlist(do.call(rbind, 
            lapply(df1$Name, function(x) replace(x, is.null(x), NA))))

names(df1)[2] <- "Measure"  

data

df1 <- structure(list(ID = c(1, 2, 2, 3, 3, 4), Name = structure(list(
NULL, structure(list(A = 10, B = 20), .Names = c("A", "B"
)), structure(list(G = 4, U = 2), .Names = c("G", "U")), 
NULL, NULL, structure(list(A = 7, B = 10), .Names = c("A", 
"B"))), class = "AsIs")), .Names = c("ID", "Name"), row.names = c(NA, 
-6L), class = "data.frame")
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks for you are instance response ,But I'm getting Below issues .please help me. Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match – muthu kutty Aug 24 '17 at 04:01
  • @muthukutty Based on the data in my post, it is working fine – akrun Aug 24 '17 at 04:05
  • But Sir I'm using almost thousands of record,That scenario it will not work. please guide me... – muthu kutty Aug 24 '17 at 04:33
  • @muthukutty If this works on 7 records, it should also work for 1000s of record. I am assuming the structure of the data is similar. You haven't shown any dput output in your post as Sotos suggested. So we are just guessing to help you – akrun Aug 24 '17 at 04:34
  • Thanks for suggestion ,you right the actual problem is list of variable is increased after certain row . can you please help me how to add a column dynamically based on List of variables increses .Ex:df1[c("Measure.A", "Measure.B,Measure.C,Measure.D,etc")] – muthu kutty Aug 24 '17 at 14:58