Reputation: 95
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.
Upvotes: 1
Views: 1300
Reputation: 101343
Be aware of that a[i,]
is a data.frame
, which can be seen from, for example
> str(a[1,])
'data.frame': 1 obs. of 3 variables:
$ X1:List of 1
..$ : chr "germline"
$ X2:List of 1
..$ : chr "somatic"
$ X3:List of 1
..$ : chr "inherited"
One workaround is using unlist(a[i,])
for (i in 1:nr) {
b[i, 1] <- paste(unlist(a[i, ]), collapse = ", ")
}
such that
> head(b)
V1
7 germline, somatic, inherited
11 germline, somatic, paternal
19 germline, somatic, maternal
35 germline, somatic, de-novo
67 germline, somatic, biparental
131 germline, somatic, uniparental
A simpler option is using do.call
+ paste
b[, 1] <- do.call(paste, c(a, sep = ", "))
which gives
> head(b)
V1
7 germline, somatic, inherited
11 germline, somatic, paternal
19 germline, somatic, maternal
35 germline, somatic, de-novo
67 germline, somatic, biparental
131 germline, somatic, uniparental
Upvotes: 1