Devang Akotia Devang Akotia - 2 months ago 7
R Question

condition does not recognize integers as number, it is considering it a character

I have a dataframe with 2 columns
The second column has one of the following values, recognized in the data frame as numbers: 0, 1, 2, or 3

I want to create a third column that has a character string values based on the values of the second column.

I tried:

df2 = data.frame(r = tssd2, cgmval = colcgm6)
df2$clrl[colcgm6 = 0] ="black"
df2$clrl[colcgm6 = 1] ="lightskyblue"
df2$clrl[colcgm6 = 2] ="blue"
df2$clrl[colcgm6 = 3] ="purple"


The error that I get is:

Error in `$<-.data.frame`(`*tmp*`, "clrl", value = character(0)) :
replacement has 0 rows, data has 4139


from the description of the error, my understanding is that the code is trying to compare the vaues of colcgm6 which are number (0 to 3) to characters 0, 1, 2, and 3. So the result is that the conditions are never true and no values are ever inputed into a new thir column.

Please help,

Edit:
For a reproducible example please use tssd as a vector of numeric values (1, 1, 1) and coldcgm6 as a vector of numeric values (0, 1, 2).

Answer

We can do this easily by using numeric indexing instead of comparing (==) 4 times

clrl <- c("black", "lightskyblue", "blue", "purple")
df2$clrl <- clrl[colcgm6+1]
head(df2)
#           r cgmval         clrl
#1 -0.7622144      1 lightskyblue
#2 -1.4290903      0        black
#3  0.3322444      2         blue
#4 -0.4690607      2         blue
#5 -0.3349868      2         blue
#6  1.5362522      3       purple

In the OP's code, instead of using the logical operator (==), the assignment operator (=) is used. By changing it, it would be fix the problem.

data

set.seed(24)
colcgm6 <- sample(0:3, 24, replace=TRUE)
tssd2 <- rnorm(24)
df2 <- data.frame(r = tssd2, cgmval = colcgm6)