rename value for multiple rows based on other variable

对着背影说爱祢 提交于 2020-06-22 04:18:27

问题


I've got a data frame with multiple rows for each participant. There is an error in some of their ids (for example, some are double). I wanted to assign them a new one like this

dat[dat$participant == "36" & dat$date == "2020-06-07_12h33.46.880"] <- "101"

I get the error message "duplicate subscripts for columns". Whats wrong with my command?

I also tried

dat$participant[dat$date== "2020-06-07_12h33.46.880"] <- "101"

with no error but also no participant with these value appearing


回答1:


The error duplicate subscripts for columns tells you here that you are trying to assign a value to a nonsensical part of dataframe. So you need to use dat$participant rather than only dat.

Generally speaking, you can use this command:

dat$participant[some condition] <- "101"

E.g. modifying your first command:

dat$participant[dat$participant == "36" & dat$date == "2020-06-07_12h33.46.880"] <- "101"

But there can be another problem, e.g. make sure you provide correct date (copy and paste the value, as there are many types of hyphens and dashes) which can occur in dirty data, maybe the IDs are not characters but numbers, etc.

So if you want more concrete help, provide here a few lines of your data frame.



来源:https://stackoverflow.com/questions/62376988/rename-value-for-multiple-rows-based-on-other-variable

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