简体   繁体   English

根据两个R中的匹配列从column2(dataframe2)中减去column1(dataframe1)

[英]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.

相关问题 根据dataframe2的列更新dataframe1的列,如果column1不为空,则创建新行 - Update column of dataframe1 based on column of dataframe2 + create new row if column1 is not empty 如何从R中的dataframe1中选择dataframe $ 1column的行中选择行 - how to select rows from a dataframe1 in R where dataframe$1column is found somewhere in dataframe2$column R循环查看dataframe1中的列值是否与dataframe2中的列值匹配 - R loop to see if column value(s) from dataframe1 match column values from dataframe2 将dataframe1转换为dataframe2,其中列的值成为新列 - Transforming dataframe1 to dataframe2 where the values of a column become new columns 从 R 中的许多列中减去数据框中的一列 - Subtract a column in a dataframe from many columns in R 如何根据dataframe1中的值从dataframe2子集并将所有子集堆叠到R中的一个数据帧中? - How to subset from dataframe2 depending on the values in dataframe1 and stack all subsets in one dataframe in R? R:根据dataframe2中相应的单元格值更新dataframe1中的单元格值 - R: updating cell values in dataframe1 based on corresponding cell values in dataframe2 R:来自 column1 的字符串匹配从 column2 的行中选择数据以创建 column3? - R: String match from column1 selects data from column2's rows to create column3? 如何从数据框中的列中减去 1? - How to subtract 1 from a column in a dataframe? 根据另一个数据帧中的匹配条件将列添加到 R 中的数据帧 - Adding column to a dataframe in R based on matching conditions in another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM