Reputation: 55
I have two table. One of them contain four column, the first column are names and other three column are numbers. I want to assign the mean of the numbers to the name.
ID number_1 number_2 number_3
A01 13 11 12
A02 18 16 17
......
The second table have a column contain the names from the first table and I want to replace the names with the mean of the numbers. For example:
v1 v2 v3 ......
8 5 A01 ......
6 3 A02 ......
replace the name so I can get:
v1 v2 v3 ......
8 5 12 ......
6 3 17 ......
I have no idea how to do this. Hope you can understand my question.
Upvotes: 0
Views: 348
Reputation: 1106
Assuming that both of your tables are dataframes and that they have the same order (i.e. A01, A02 etc.)
df1 <- read.table(text = "ID number_1 number_2 number_3
A01 13 11 12
A02 18 16 17",
header = T)
df2 <- read.table(text = "v1 v2 v3
8 5 A01
6 3 A02",
header = T)
df1means <- rowMeans(df1[2:4])
df2$v3 <- df1means
df2
# v1 v2 v3
# 1 8 5 12
# 2 6 3 17
Upvotes: 1