I have a question on how to use the paste function in a list if its vectors are of different lengths. I have a dataframe with columns of different lengths, such as:
df = data.frame(Set1=c("abc",'def',"ghi","jkl",""), Set2=c("abc",'def',"","",""), Set3=c("mno","pqr","stu","vwx","xyz"))
I tried to make a list a.list based on each column and collapse each factor with a ,. Something like
a.list
$Set1
[1] abc,def,ghi,jkl
$Set2
[1] abc,def
$Set3
[1] mno,pqr,stu,vwx,xyz
I tried
> a.list = apply(df, 2, as.list)
> a.list = lapply(a.list, function(x) paste(x, collapse=",")
However, it collapsed the factors with , to the maximum length of the list. For example,
a.list
$Set1
[1] "abc,def,ghi,jkl,"
$Set2
[1] "abc,def,,,"
Sorry it may be a simple question. I am still learning R. Thanks!