Replace values ββin data.frame with values ββfrom another data.frame
So I have this data.frame, call it df1, which looks like this:
r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12
1 1 2 2 2 0 2 1 2 0 2 1 2
2 0 2 2 2 2 2 0 2 0 2 2 2
3 1 2 2 2 0 2 2 2 2 1 2 2
4 1 2 2 2 2 1 2 2 2 1 2 2
5 0 1 2 2 0 2 1 0 0 1 1 0
6 0 2 0 2 0 2 2 0 0 2 2 2
7 1 2 2 2 2 2 2 0 0 1 2 2
8 0 2 2 2 2 2 2 2 2 2 2 2
9 0 2 2 2 2 2 1 1 1 1 1 2
and one, let's call it df2, which looks like this:
r1 r2 r3 r4 r5 ...
1 arp1_base melo1_base son_clave melo2_base melo3_major ...
2 arp1_v1 melo1_v1 rumba_clave melo2_staccato melo3_minor ...
3 arp1_v2 melo1_v2 rumba_clave_v1 melo2_contour melo3_contour ...
I am not inserting all columns here, but you get the idea (and yes, the number of rows is different).
Now I need to replace the numbers in each column of the first data.frame with the value from the second data.frame corresponding to the column and using the numbers in the first data.frame as the row index for the second data. Frame. I would like to have a new data.frame that looks like this:
r1 r2 r3 ...
1 arp1_v1 melo1_v2 rumba_clave_v1 ...
2 arp1_base melo1_v2 rumba_clave_v1 ...
3 arp1_v1 melo1_v2 rumba_clave_v1 ...
. . . .
. . . .
. . . .
How should I do it? Ideally, each new column will be a factor that stores the three names as levels.
And by the way, I feel like this is sort of an FAQ, but I ran into the usual n00b problem with no idea what to do with Google. Is there some magic around this?
EDIT As suggested by @akrun below I can do df1[] <- Map(function(x, y) factor(y[x+1]), df1, df2)
and I get almost what I want in the sense that the columns df1
are now factors with substituted values, but I need each of these factors to have as levels all the values ββfrom corresponding column in df2
.
source to share
Try
df1[] <- Map(function(x, y) y[x+1], df1, df2)
df1
# r1 r2 r3 r4 r5
#1 arp1_v1 melo1_v2 rumba_clave_v1 melo2_contour melo3_major
#2 arp1_base melo1_v2 rumba_clave_v1 melo2_contour melo3_contour
#3 arp1_v1 melo1_v2 rumba_clave_v1 melo2_contour melo3_major
#4 arp1_v1 melo1_v2 rumba_clave_v1 melo2_contour melo3_contour
#5 arp1_base melo1_v1 rumba_clave_v1 melo2_contour melo3_major
#6 arp1_base melo1_v2 son_clave melo2_contour melo3_major
#7 arp1_v1 melo1_v2 rumba_clave_v1 melo2_contour melo3_contour
#8 arp1_base melo1_v2 rumba_clave_v1 melo2_contour melo3_contour
#9 arp1_base melo1_v2 rumba_clave_v1 melo2_contour melo3_contour
Update
To create columns as factor
with levels equal to the levels
corresponding columnsdf2
df1[] <- Map(function(x, y) factor(y[x+1], levels=unique(y)), df1, df2)
levels(df1[,1])
#[1] "arp1_base" "arp1_v1" "arp1_v2"
data
df1 <- structure(list(r1 = c(1L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L), r2 = c(2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L), r3 = c(2L, 2L, 2L, 2L, 2L, 0L,
2L, 2L, 2L), r4 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), r5 = c(0L,
2L, 0L, 2L, 0L, 0L, 2L, 2L, 2L)), .Names = c("r1", "r2", "r3",
"r4", "r5"), class = "data.frame", row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9"))
df2 <- structure(list(r1 = c("arp1_base", "arp1_v1", "arp1_v2"),
r2 = c("melo1_base", "melo1_v1", "melo1_v2"), r3 = c("son_clave",
"rumba_clave", "rumba_clave_v1"), r4 = c("melo2_base", "melo2_staccato",
"melo2_contour"), r5 = c("melo3_major","melo3_minor", "melo3_contour")),
.Names = c("r1", "r2", "r3", "r4", "r5"), class = "data.frame",
row.names = c("1", "2", "3"))
source to share