Reputation: 45
Data frame = qog_std3
factor = btid
I am trying to collapse this ordinal level factor using following code:
I get the following error message:
Error: unexpected '=' in:
"btid4 <- fct_collapse(qog_std3$btid,
1="
Can anyone explain to me why the use of "=" provides this error and what I can do about it?
Any alternative solution would also be deeply appreciated.
Upvotes: 1
Views: 447
Reputation: 887911
If the column is factor
or character
, we need to quote the name especically when it is numeric. It is not an issue when it is non-numeric
fct_collapse(df1$btid, "1" = c("1", "2"))
#[1] 1 1 3 3 4 5 1 1
#Levels: 1 3 4 5
It can be also backquotes
fct_collapse(df1$btid, `1` = c("1", "2"))
#[1] 1 1 3 3 4 5 1 1
#Levels: 1 3 4 5
whereas if we specify the unquoted numeric value
fct_collapse(df1$btid, 1 = c("1", "2"))
Error: unexpected '=' in " fct_collapse(df1$btid, 1 ="
However, this is not an issue when it is character
fct_collapse(df1$id2, AB = c("A", "B"))
#[1] AB AB C D AB AB C AB
#Levels: AB C D
df1 <- structure(list(btid = c("1", "1", "3", "3", "4", "5", "1", "2"
), id2 = c("A", "B", "C", "D", "A", "B", "C", "A")), row.names = c(NA,
-8L), class = "data.frame")
Upvotes: 3