I am trying to change multiple elements of a data frame, concatenate all columns for each row and write them into a new data frame in R. To change multiple elements using dictionary, I followed Ramnath's solution in here.
You can see my sample code below:
arr <- c(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 1073741824)
dict = list('1' = 'germline', '2' = 'somatic', '4' = 'inherited', '8' = 'paternal',
'16' = 'maternal', '32' = 'de-novo', '64' = 'biparental', '128' = 'uniparental',
'256' = 'not-tested', '512' = 'tested-inconclusive', '1024' = 'not-reported',
'1073741824' = 'other')
a <- data.frame(t(combn(arr, 3)), stringsAsFactors = FALSE)
nr = nrow(a)
b <- as.data.frame(matrix(nrow = nr, ncol = 1))
row.names(b) <- rowSums(a)
a[] <- lapply(a, as.character)
for (j in 1:length(dict)) {
a <- replace(a, a == names(dict[j]), dict[j])
}
for (i in 1:nr) {
b[i, 1] <- paste(as.character(as.vector(a[i,])), collapse = ", ")
}
My expected output is (example):
> b[1,1]
[1] germline, somatic, inherited
However, I get this:
> b[1,1]
[1] "list(\"germline\"), list(\"somatic\"), list(\"inherited\")"
I couldn't figure out what problem is, and I would be very pleased if you could help me with it.