I have assigned a lists to inside the loop. The lists has many dataframe of unequal length and different column names. But even though I convert lists to dataframe, the output is still coming as lists only . Below is my code
fv <- structure(list(values = c("A&C", "A&E", "C&E"), v1 = c("A", "A",
"C"), v2 = c("C", "E", "E")), row.names = c(NA, 3L), class =
"data.frame")
df <- structure(list(A = structure(c(1L, 4L, 6L, 1L, 8L, 2L, 7L, 3L,
5L, 5L, 1L, 8L, 2L, 7L, 2L), .Label = c("asd", "dfg", "fgdsgd",
"fsd", "gdfgd", "gs", "sdfg", "sf"), class = "factor"), B = c(29L,
24L, 46L, 50L, 43L, 29L, 32L, 24L, 35L, 39L, 33L, 47L, 53L, 26L,
31L), C = structure(c(8L, 5L, 1L, 6L, 3L, 2L, 9L, 7L, 6L, 3L,
2L, 9L, 8L, 8L, 4L), .Label = c("asd", "er", "fg", "gf", "gfd",
"gfg", "qw", "sf", "tr"), class = "factor"), D = c(36L, 56L,
39L, 26L, 56L, 35L, 27L, 31L, 33L, 45L, 34L, 27L, 43L, 40L, 56L
), E = structure(c(8L, 5L, 1L, 6L, 3L, 2L, 9L, 7L, 6L, 3L, 2L,
9L, 8L, 8L, 4L), .Label = c("asd", "er", "fg", "gf", "gfd", "gfg",
"qw", "sf", "tr"), class = "factor"), F = c(44L, 34L, 37L, 23L,
37L, 51L, 28L, 36L, 33L, 31L, 39L, 43L, 25L, 37L, 43L)), class =
"data.frame", row.names = c(NA,
-15L))
io <- list()
asd <- list()
for(i in 1:nrow(fv))
{ io[[i]] <- tapply(df[,fv$v1[i]], df[,fv$v2[i]], FUN = function(x)
length(unique(x)))
}
for(i in 1:nrow(fv))
{ asd[[i]] <- stack(unlist(io[i]))
colnames(asd[[i]]) <- c(fv$v1[i],fv$v2[i])
asd[[i]] <- data.frame(asd[[i]]) }
DO not worry about the data objects above. The code is all good expect last line asd[[i]] <- data.frame(asd[[i]]). Because below is the output i get.Its a lists and I need it in a data frame. There are 2 examples below
asd[2]
[[1]]
A E
1 1 asd
2 2 er
3 2 fg
4 1 gf
5 1 gfd
6 2 gfg
7 1 qw
8 3 sf
9 2 tr
asd[1]
[[1]]
A C
1 1 asd
2 2 er
3 2 fg
4 1 gf
5 1 gfd
6 2 gfg
7 1 qw
8 3 sf
9 2 tr
Instead I Need like this a dataframe. Hope my points are clear
asd[2]
A E
1 1 asd
2 2 er
3 2 fg
4 1 gf
5 1 gfd
6 2 gfg
7 1 qw
8 3 sf
9 2 tr
asd[1]
A C
1 1 asd
2 2 er
3 2 fg
4 1 gf
5 1 gfd
6 2 gfg
7 1 qw
8 3 sf
9 2 tr