Goal:
I want to turn the values in t1 and t2 from df data
into some other value (PWT) that is in a lookup table lookup
. I've seen some tutorials on how to do this for a single column, but I want to do it programmatically for an indefinite number of t
columns (e.g. t1, t2, t3, t4, t5, ...) from data
.
Lookup
# A tibble: 6 x 4 (HEAD)
Response `Final Fil.` Adjustment PWT
<chr> <dbl> <dbl> <dbl>
1 00000 9.00 0.500 9.50
2 00001 9.00 -0.500 8.50
3 00010 7.00 0.500 7.50
4 00011 7.00 -0.500 6.50
5 00100 7.00 0.500 7.50
6 00101 7.00 -0.500 6.50
lookup
w/ dput
structure(list(Response = c("00000", "00001", "00010", "00011",
"00100", "00101", "00110", "00111", "01000", "01001", "01010",
"01011", "01100", "01101", "01110", "01111", "10000", "10001",
"10010", "10011", "10100", "10101", "10110", "10111", "11000",
"11001", "11010", "11011", "11100", "11101", "11110", "1111"),
`Final Fil.` = c(9, 9, 7, 7, 7, 7, 5, 5, 7, 7, 5, 5, 5, 5,
3, 3, 7, 7, 5, 5, 5, 5, 3, 3, 5, 5, 3, 3, 3, 3, 1, 2), Adjustment = c(0.5,
-0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5,
0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5,
-0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, -0.5), PWT = c(9.5,
8.5, 7.5, 6.5, 7.5, 6.5, 5.5, 4.5, 7.5, 6.5, 5.5, 4.5, 5.5,
4.5, 3.5, 2.5, 7.5, 6.5, 5.5, 4.5, 5.5, 4.5, 3.5, 2.5, 5.5,
4.5, 3.5, 2.5, 3.5, 2.5, 1.5, 1.5)), .Names = c("Response",
"Final Fil.", "Adjustment", "PWT"), row.names = c(NA, -32L), class = c("tbl_df",
"tbl", "data.frame"))
Data
# A tibble: 6 x 4 (HEAD)
Mouse Group t1 t2
<dbl> <chr> <chr> <chr>
1 1.00 SNI 00011 00000
2 2.00 Sham 00011 00001
3 3.00 SNI 00000 00001
4 4.00 Sham 00110 00000
5 5.00 SNI 00001 00001
6 6.00 Sham 00010 00101
data
w/ dput
structure(list(Mouse = c(1, 2, 3, 4, 5, 6, 7, 8), Group = c("SNI",
"Sham", "SNI", "Sham", "SNI", "Sham", "SNI", "Sham"), t1 = c("00011",
"00011", "00000", "00110", "00001", "00010", "01001", "00110"
), t2 = c("00000", "00001", "00001", "00000", "00001", "00101",
"00100", "00010")), .Names = c("Mouse", "Group", "t1", "t2"), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))
I was able to do this for t1
in data
using this code:
indices <- (match(x = data$t1, table = lookup$Response))
response <- mutate(data, t1=lookup$PWT[indices])
Output in new table response
Mouse Group t1 t2 t3 t4 t5 t6
<dbl> <chr> <dbl> <chr> <lgl> <lgl> <lgl> <lgl>
1 1.00 SNI 6.50 00000 NA NA NA NA
2 2.00 Sham 6.50 00001 NA NA NA NA
3 3.00 SNI 9.50 00001 NA NA NA NA
4 4.00 Sham 5.50 00000 NA NA NA NA
5 5.00 SNI 8.50 00001 NA NA NA NA
6 6.00 Sham 7.50 00101
I'm looking now to do this more programmatically for as many t-columns as I have.