0
votes

I was planning to make a new column with named vector which can match the names in dataframe, but it seems the matching is not done properly.

Here's an example code

vector <- c(APPLE=10,BANANA=2,KIWI=3,CARROT=4,ORANGE=5)

df <- data.frame(fru=c("APPLE","KIWI","CARROT","CARROT","KIWI","ORANGE","BANANA"))
df$num <- vector[df$fru]

in this code, the vector is defined like this,

 APPLE BANANA   KIWI CARROT ORANGE 
     1      2      3      4      5 

but in the dataframe, the number corresponding to KIWI and CARROT doesn't match...

     fru num
1  APPLE  10
2   KIWI   4
3 CARROT   3
4 CARROT   3
5   KIWI   4
6 ORANGE   5
7 BANANA   2

is there any misunderstanding in the way that i make indexing in vector?

2

2 Answers

1
votes

It is possible that the 'fru' column is factor. It needs to be character class.

vector[as.character(df$fru)]
#  APPLE   KIWI CARROT CARROT   KIWI ORANGE BANANA 
#   10      3      4      4      3      5      2 

From R 4.0 onwards, by default, stringsAsFactors = FALSE while we create data.frame (or read with read.csv/read.table etc.). If the version of R is less than 4, then the stringsAsFactors = TRUE by default. An option is to specify the argument as FALSE while constructing the data.frame or convert to character with as.character

0
votes

I copied your verbatim code at my RStudio console, and the corresponding numbers for KIWI and CARROTS in df match the numbers in vector . I don't know how you got different result.

enter image description here