An easier option is to convert to factor
with levels
specified as unique
elements of the column and coerce to integer with as.integer
df1$A <- as.integer(factor(df1$stimulus, levels = unique(df1$stimulus)))
Or use match
on the unique
values
df1$A <- with(df1, match(stimulus, unique(stimulus)))
-Update
If it needs to be custom values, an option is also to specify the labels
along with levels
df1$A <- with(df, as.numeric(as.character(factor(stimulus,
levels = c("S4", "S9", "S5", "S10"), c("1.1", "2.2", "3.2" , "5.8")))))
Or a better option may to do a join/merge with a key/value dataset
keydat <- data.frame(stimulus = c("S4", "S9", "S5", "S10"),
A = c(1.1, 2.2, 3.2, 5.8))
df2 <- merge(df1, keydat, all.x = TRUE)