-1

There are objects with basic datas like a<-list(a=1,b="A",c=character())

Now I want convert it to a data.frame, but there for I need equal rows. How to fill the empty vectors with NA in easy way to run as.data.frame(a)? the only Idea I have is to ask if one elment of the list has length<1 then set element[1]=NA.

Klaus
  • 1,946
  • 3
  • 19
  • 34
  • Okay I see data.frame(t(a)) is a good way, but how to replace the empty strings with NA or how to ask if the row in the data frame has empty entry? Later for I like to have the functionality which I get for `na.omit(df)`. – Klaus Sep 23 '13 at 12:59
  • Can we help you find sources of basic usage in `R`? This question would appear to suggest you're not familiar with simple functions like `length(a[[1]])` or `a[[2]][5:length(a[[1]])] <- NA` – Carl Witthoft Sep 23 '13 at 14:21
  • @CarlWitthoft, sry I am only looking for a better way to express `b<-lapply(a,function(x) if(length(x)<1) NA else x); data.frame(b)`. – Klaus Sep 23 '13 at 14:25

1 Answers1

1

I'm not sure this is any cleaner, but it does get rid of the if stuff:

lfoo<-list(one=1:3,two=character(),three=4:6,four=vector())
dfoo<-dfoo<-data.frame(one=rep(NA,3),two=rep(NA,3), three=rep(NA,3),four=rep(NA,3))
lvalues <-  which(unlist(lapply(1:4,function(x) length(lfoo[[x]]) > 0))
for (j in lvalues) dfoo[,jvalues]<-lfoo[[jvalues]]

This may point you to simple ways of dealing with conversions and selective replacements.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73