Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (11.4k points)

I am new to R programming and I have imported a CSV file. I would like to change the variable value for symbol column. If the value is "DEF" then "NEW1". If the value is "GHI" then "NEW2". I tried the below code but, I am getting a warning message and I checked the dataframe contains "NA" after changing.

df$symbol[df$symbol == "DEF"] <- "NEW1"
df$symbol[df$symbol == "GHI"] <- "NEW2"

Warning message:


In `[<-.factor`(`*tmp*`, df$symbol == "DEF",  :
  invalid factor level, NA generated

1 Answer

0 votes
by (32.3k points)

I would suggest you to simply try this :

df$symbol <- as.character(df$symbol)

df$symbol[df$symbol == "DEF"] <-"NEW1"

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...