[英]subtract column1 (dataframe1) from column2 (dataframe2) based on matching column in both R
Dataframe1
has two columns: num_movies and userId. Dataframe1
有两列:num_movies和userId。 Dataframe2
has two columns: No_movies and userId. Dataframe2
有两列:No_movies和userId。 But Dataframe2
has 2106 rows and Dataframe1
has 1679 rows. 但是
Dataframe2
有2106行,而Dataframe1
有1679行。 I want to subtract the number of movies in Dataframe2
from Dataframe1
based on matching userId values. 我想根据匹配的userId值从
Dataframe1
减去Dataframe2
中电影的数量。 I have written the following line: 我写了以下几行:
df1$num_movies = df1$num_movies - df2$No_movies[df1$userId %in% df2$userId]
and I get the following error: 我收到以下错误:
Error in `$<-.data.frame`(`*tmp*`, "num_movies", value = c(2, 9, 743, :
replacement has 2106 rows, data has 1679
In addition: Warning message:
In df1$num_movies - df2$No_movies[df1$userId %in% :
longer object length is not a multiple of shorter object length
Elsewhere it has been proposed that I upgrade from 3.0.2 to 3.1.2 to solve this problem. 在其他地方 ,有人建议我从3.0.2升级到3.1.2以解决此问题。 But I still have the same error after the upgrade.
但是升级后我仍然有同样的错误。 What I have written seems logical for me.
我写的东西对我来说似乎很合逻辑。 I intend to pick only 1679 userIds out of 2106. Why is it selecting all of them?
我打算从2106个用户中仅选择1679个用户ID。为什么要选择所有这些用户ID? How do I circumvent this error?
如何避免此错误?
You can use the match
function to find the corresponding row from Dataframe2
for each row in Dataframe1
. 您可以使用
match
功能,找到相应的行Dataframe2
在每一行Dataframe1
。
matched.movies <- Dataframe2$No_movies[match(Dataframe1$userId, Dataframe2$userId)]
matched.movies[is.na(matched.movies)] <- 0
Dataframe1$num_movies <- Dataframe1$num_movies - matched.movies
Dataframe1
# num_movies userId
# 1 10 1
# 2 7 2
# 3 6 3
Data: 数据:
(Dataframe1 <- data.frame(num_movies=rep(10, 3), userId=1:3))
# num_movies userId
# 1 10 1
# 2 10 2
# 3 10 3
(Dataframe2 <- data.frame(No_movies=2:6, userId=c(0, 2, 3, 9, 10)))
# No_movies userId
# 1 2 0
# 2 3 2
# 3 4 3
# 4 5 9
# 5 6 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.