I'll start by saying I'm trying to learn r but it doesn't come easy to me. Similar to this post here I am trying to match values in multiple columns from one data frame (df) and then replace those values based on the corresponding columns from the other data frame (df.key). Here is my example df:
name type place ttotal t01 t02 t03 t04 t05 t06 t07 t08 t09
joe cat SE 7 3 2 2 3 2 5 2 0 1
john cat SE 2 0 0 4 0 3 1 3 1 7
sue cat SE 1 2 0 5 0 4 1 4 3 0
jack cat SE 6 3 4 2 2 4 0 2 1 5
Below is my df.key to be used to match the values above in columns df$ttotal to t09 with df.key$class and replace with the values in df.key$mid accordingly:
lo hi class mid
0 0 0 0.0
0 1 1 0.5
1 2 2 3.0
5 10 3 7.5
10 20 4 15.0
20 30 5 25.0
30 40 6 35.0
40 50 7 45.0
so the first row should be:
name type place ttotal t01 t02 t03 t04 t05 t06 t07 t08 t09
joe cat SE 45.0 7.5 3.0 3.0 7.5 3.0 25.0 3.0 0.0 0.5
Here is just one match loop I tried but it populates the came value across the row:
for(i in 1:dim(df)[1]){
for(j in df$4:13) {
df[i,j] <- df.key$mid[match(i, df.key$class)]
}
}
Thanks for the help. I'd like to try to get a solution somewhat similar to this in hopes I can understand it.