I want to create one dataframe(result df) from two dataframes(df1, df2).
How do I make the 'result df' below?
df1 <- data.frame('user'=c('user1', 'user2', 'user3'),
'item1'=c(10,5,1),
'item2'=c(5,3,8),
'item3'=c(1,7,10))
df2 <- data.frame('user'=c('user1', 'user2', 'user3'),
'ratio'=c(0.5,0.3,0.2))
Try the code below
> colSums(df1[-1]*df2$ratio)
item1 item2 item3
6.7 5.0 4.6
You need to convert your data frames into matrices
df1 <- data.frame('user'=c('user1', 'user2', 'user3'),
'item1'=c(10,5,1),
'item2'=c(5,3,7),
'item3'=c(1,8,10))
df2 <- data.frame('user'=c('user1', 'user2', 'user3'),
'ratio'=c(0.5,0.3,0.2))
res <- as.numeric(as.matrix(df1[,-1])%*%as.matrix(df2[,2]))
names(res) <- c("item1","item2", "item3")
#Output
res
item1 item2 item3
6.7 5.0 4.6
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.