Reputation: 2584
I have a data like this
df<- list(structure(list(A = c(0.1807, 0.2369, 0.2432, 0.2772, 0.2833,
0.3194, 0.407, 0.4507, 0.5023, 0.5064, 0.5183, 0.5414, 0.5527
), B = c(0.1139, 0.1417, 0.1028, 0.2472, 0.025, 0.2917, 0.3917,
0.4417, 0.4472, 0.4639, 0.5028, 0.5194, 0.5305)), .Names = c("A",
"B"), row.names = c(NA, -13L), class = "data.frame"), structure(list(
A = c(0.1807, 0.2369, 0.2432, 0.2772, 0.2833, 0.3194, 0.407,
0.4507, 0.5023), B = c(0.1139, 0.1417, 0.1028, 0.2472, 0.025,
0.2917, 0.3917, 0.4417, 0.4472)), .Names = c("A", "B"), row.names = c(NA,
-9L), class = "data.frame"), structure(list(A = c(0.1807, 0.2369,
0.2432, 0.2772), B = c(0.1139, 0.1417, 0.1028, 0.2472)), .Names = c("A",
"B"), row.names = c(NA, -4L), class = "data.frame"))
I want to have the output like this
A B A B A B
0.1807 0.1139 0.1807 0.1139 0.1807 0.1139
0.2369 0.1417 0.2369 0.1417 0.2369 0.1417
0.2432 0.1028 0.2432 0.1028 0.2432 0.1028
0.2772 0.2472 0.2772 0.2472 0.2772 0.2472
0.2833 0.025 0.2833 0.025
0.3194 0.2917 0.3194 0.2917
0.407 0.3917 0.407 0.3917
0.4507 0.4417 0.4507 0.4417
0.5023 0.4472 0.5023 0.4472
0.5064 0.4639
0.5183 0.5028
0.5414 0.5194
0.5527 0.5305
basically putting each list aside of the other one (although they are different in size)
Upvotes: 1
Views: 95
Reputation: 42544
The code below will produce something which looks very similar to your expected output:
library(data.table)
cols <- colnames(df[[1L]])
long <- rbindlist(df, idcol = "df.id")
wide <- dcast(long, rowid(df.id) ~ df.id, as.character, value.var = cols, fill = "")[
, .SD, .SDcols = as.vector(outer(cols, seq_along(df), paste, sep = "_"))]
setnames(wide, rep(cols, length(df)))
wide
A B A B A B 1: 0.1807 0.1139 0.1807 0.1139 0.1807 0.1139 2: 0.2369 0.1417 0.2369 0.1417 0.2369 0.1417 3: 0.2432 0.1028 0.2432 0.1028 0.2432 0.1028 4: 0.2772 0.2472 0.2772 0.2472 0.2772 0.2472 5: 0.2833 0.025 0.2833 0.025 6: 0.3194 0.2917 0.3194 0.2917 7: 0.407 0.3917 0.407 0.3917 8: 0.4507 0.4417 0.4507 0.4417 9: 0.5023 0.4472 0.5023 0.4472 10: 0.5064 0.4639 11: 0.5183 0.5028 12: 0.5414 0.5194 13: 0.5527 0.5305
Upvotes: 1
Reputation: 887058
We can use cbind.fill
from rowr
library(rowr)
res <- do.call(cbind.fill, c(df, fill=NA))
res
# A B A B A B
#1 0.1807 0.1139 0.1807 0.1139 0.1807 0.1139
#2 0.2369 0.1417 0.2369 0.1417 0.2369 0.1417
#3 0.2432 0.1028 0.2432 0.1028 0.2432 0.1028
#4 0.2772 0.2472 0.2772 0.2472 0.2772 0.2472
#5 0.2833 0.0250 0.2833 0.0250 NA NA
#6 0.3194 0.2917 0.3194 0.2917 NA NA
#7 0.4070 0.3917 0.4070 0.3917 NA NA
#8 0.4507 0.4417 0.4507 0.4417 NA NA
#9 0.5023 0.4472 0.5023 0.4472 NA NA
#10 0.5064 0.4639 NA NA NA NA
#11 0.5183 0.5028 NA NA NA NA
#12 0.5414 0.5194 NA NA NA NA
#13 0.5527 0.5305 NA NA NA NA
It is better to have column names unique in the dataset
colnames(res) <- make.unique(colnames(res))
Also, the missing values are NA
. If we need to ""
, change fill=NA
to fill=""
) i.e.
do.call(cbind.fill, c(df, fill=''))
, but that will also change the class
of the columns to character/factor
Upvotes: 2
Reputation: 109
this is impossible. data.frame is a kind of list where all the elements of the list are stuck together as columns and must be of the same length. more info R dataframe with varied column lengths
Upvotes: 0