ifelse statements with dataframes of different lengths

只谈情不闲聊 提交于 2019-12-25 02:07:13

问题


I have two dataframes. One that has a list of unique ID numbers (think customer names and demographic data) and another dataframe with a list of transaction data (think purchase data, $ amount, etc) where the same unique ID number is also a column.

I'd like to create a dummy variable using a nested ifelse statement that searches the transaction dataframe using the unique ID and then check if a second attribute matches between the two dataframes.

For example:

data.frame3$dummy_variable <- ifelse(data.frame1$id == data.frame2$id,
ifelse(data.frame1$attributeX == data.frame2$attritubeX, 1, 0)
,2)

However, data.frame1 and data.frame2 have different row lengths, so I get an error message: "longer object length is not a multiple of shorter object length".

These data.frames cannot be the same length. Is there another way to attack this?

来源:https://stackoverflow.com/questions/29529552/ifelse-statements-with-dataframes-of-different-lengths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!