My data table dfhas a column subject(for example, "SubjectA", "SubjectB", ...). Each question answers many questions, and the table is in a long format, so for each object there are many rows. The thematic column is a factor. I want to create a new column - name it subject.id- it's just a numeric version subject. Therefore, for all lines with "SubjectA" this will be 1; for all lines with "SubjectB" this will be 2; and etc.
I know that a simple way to do this with help dplyrwould be to call df %>% mutate(subject.id = as.numeric(subject)). But I tried to do it like this:
subj.list <- unique(as.character(df$subject))
df %>% mutate(subject.id = which(as.character(subject) == subj.list))
And I get this error:
Error: wrong result size (12), expected 72 or 1
Why is this happening? I am not interested in other ways to solve this particular problem. Rather, I worry that my inability to understand this error reflects a deep misunderstanding of dplyreither mutate. I understand that this call should be conceptually equivalent:
df$subject.id <- NULL
for (i in 1:nrow(df)) {
df$subject.id[i] <- which(as.character(df$subject[i]) == subj.list))
}
But the latter works, and the first does not. Why?
Playable example:
df <- InsectSprays %>% rename(subject = spray)
subj.list <- unique(as.character(df$subject))
df$subject.id <- NULL
for (i in 1:nrow(df)) {
df$subject.id[i] <- which(as.character(df$subject[i]) == subj.list)
}
df %>% mutate(subject.id = which(as.character(subject) == subj.list))
source
share