[Solved] Replacing row elements in a dataframe based on values from another dataframe [duplicate]


Here’s a stab:


tableresults <- read.table(header=TRUE, stringsAsFactors=FALSE, text="
  ACTIVITY_X ACTIVITY_Y ACTIVITY_Z winning_cluster
1         19         21         28        cluster3
2         20         14         24        cluster3
3         34         35         49        cluster3
4         18          5         19        cluster2
5         23         27         35        cluster3
6         33         20         39        cluster3")

averagetable <- read.table(header=TRUE, stringsAsFactors=FALSE, text="
   Group.1  Standing
1 cluster1  0.5642857
2 cluster2  0.7795848
3 cluster3  0.7922980")

averagetable$x <- c("Standing", "Moving/Feeding", "Feeding/Moving")[ rank(-averagetable$Standing) ]
merge(tableresults, averagetable[,c(1,3)], by.x="winning_cluster", by.y="Group.1")
#   winning_cluster ACTIVITY_X ACTIVITY_Y ACTIVITY_Z              x
# 1        cluster2         18          5         19 Moving/Feeding
# 2        cluster3         19         21         28       Standing
# 3        cluster3         20         14         24       Standing
# 4        cluster3         34         35         49       Standing
# 5        cluster3         23         27         35       Standing
# 6        cluster3         33         20         39       Standing

6

solved Replacing row elements in a dataframe based on values from another dataframe [duplicate]