问题
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