I have a list and I would like to convert it into a data.frame with two columns. The problem is that the length of list elements is not equal, here is a example of how my data looks:
my.list
$A1CF
[1] "A1CF" "APOBEC1" "CUGBP2" "KHSRP" "SYNCRIP" "TNPO2"
$A2LD1
[1] "A2LD1" "PRPSAP2" "RPL15" "TANC1"
$A2M
[1] "A2M" "ADAM19" "ADAMTS1" "AMBP" "ANXA6" "APOE"
This list comes from a previous data.frame:
my.list <- split(df$V2, df$V1)
df
V1 V2
1 A1BG A1BG
2 A1BG CRISP3
3 A1CF A1CF
4 A1CF APOBEC1
5 A1CF CUGBP2
6 A1CF KHSRP
7 A1CF SYNCRIP
8 A1CF TNPO2
9 A2LD1 A2LD1
10 A2LD1 PRPSAP2
11 A2LD1 RPL15
12 A2LD1 TANC1
13 A2M A2M
14 A2M ADAM19
15 A2M ADAMTS1
16 A2M AMBP
17 A2M ANXA6
18 A2M APOE
where the elements corresponding to AB1G were removed. I would like to revert the split effect to obtain the same structure:
new.df
A1CF A1CF
A1CF APOBEC1
A1CF CUGBP2
A1CF KHSRP
A1CF SYNCRIP
A1CF TNPO2
A2LD1 A2LD1
A2LD1 PRPSAP2
A2LD1 RPL15
A2LD1 TANC1
A2M A2M
A2M ADAM19
A2M ADAMTS1
A2M AMBP
A2M ANXA6
A2M APOE
I have tried with: df.new <- do.call(rbind, my.list)
, but obviously it didn't work.
Many thanks
The inverse of the split method is join . You choose a desired separator string, (often called the glue) and join the list with the glue between each of the elements. The list that you glue together ( wds in this example) is not modified. Also, you can use empty glue or multi-character strings as glue.
A split function is composed of a specified separator and max parameter. A split function can be used to split strings with the help of a delimiter. A split function can be used to split strings with the help of the occurrence of a character. A split function can be used to split strings in the form of a list.
The other fundamental string operation is the opposite of splitting strings: string concatenation.
Using these dummy data,
ll <- list(a = letters[3:6],
b = LETTERS[1:10],
c = letters[1:2])
stack(ll)
or
reshape2::melt(ll, id=1)
or
plyr::ldply(ll, cbind)
should give you roughly the right format
If you have a list of vectors, @baptiste provides more concise and general answers for list.
You can also apply unsplit
, the inverse operation of split
.
options(stringsAsFactors = FALSE)
df <- data.frame(V1 = c('A1BG', 'A1BG', 'A1CF', 'A1CF', 'A1CF', 'A1CF', 'A1CF', 'A1CF',
'A2LD1', 'A2LD1', 'A2LD1', 'A2LD1', 'A2M', 'A2M', 'A2M', 'A2M',
'A2M', 'A2M'),
V2 = c('A1BG', 'CRISP3', 'A1CF', 'APOBEC1', 'CUGBP2', 'KHSRP', 'SYNCRIP',
'TNPO2', 'A2LD1', 'PRPSAP2', 'RPL15', 'TANC1', 'A2M', 'ADAM19',
'ADAMTS1', 'AMBP', 'ANXA6', 'APOE'))
my.list <- split(df$V2, df$V1)
newdat <- data.frame(V1=df$V1, V2=unsplit(my.list, df$V1))
If you have a list of data.frame from split, unsplit
should be appropriate.
my.list <- split(df, df$V1)
newdf <- unsplit(my.list[!names(my.list) %in% 'A1BG'], df$V1[!df$V1 %in% 'A1BG'])
or
newdf <- do.call(rbind, my.list[!names(my.list) %in% 'A1BG'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With