How to change the value of a variable using R programming in a data frame?

0 votes

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
Jan 11 in Data Analytics by Sophie may
• 9,530 points
169 views

2 answers to this question.

0 votes

Try this:

df$symbol <- as.character(df$symbol)
df$symbol[df$symbol == "DEF"] <-"NEW1"
answered Jan 11 by Tyrion anex
• 8,270 points
0 votes
You are getting this error since the fields are saved as factors in the r data frame, so either convert like @Tyrion anex said after storing the data frame or while fetching the CSV into data frame by using stringsAsFactors = FALSE like below.

ABC = read.csv("C:\\Users\\Cherukuri_Sindhu\\Downloads\\abc.csv",stringsAsFactors = FALSE)
answered Aug 16 by anonymous
• 25,660 points

Related Questions In Data Analytics

0 votes
1 answer

How to replace all occurrences of a character in a character column in a data frame in R

If you used sub() to replace the ...READ MORE

answered Jun 28 in Data Analytics by anonymous
• 25,660 points
48 views
0 votes
1 answer
0 votes
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21 in Data Analytics by anonymous
• 25,660 points
119 views
0 votes
2 answers

How to change column names of a Data frame?

Try this way - names(data frame) <- ...READ MORE

answered Aug 26 in Data Analytics by anonymous
• 25,660 points
69 views
0 votes
1 answer

Parallel programming In R using GPU

Check out the CRAN Task View on High-Performance ...READ MORE

answered Feb 4 in Data Analytics by Tyrion anex
• 8,270 points
58 views
0 votes
1 answer

R programming: Naming the output file using a variable

Use the paste command: write.csv(max.hsi, paste0("Index_", i,".csv" ...READ MORE

answered Mar 25 in Data Analytics by Sophie may
• 9,530 points
29 views
0 votes
1 answer
0 votes
1 answer

Changing variable values using R programming

This should work: df$symbol <- as.character(df$symbol) df$symbol[df$symbol == "ABCD.BO"] ...READ MORE

answered May 27 in Data Analytics by Zulaikha
• 840 points
23 views
0 votes
1 answer

R programming: How to subset data and plot graphs in R?

You can create a grouping variable depending ...READ MORE

answered Feb 18 in Data Analytics by Tyrion anex
• 8,270 points
55 views
0 votes
1 answer

R programming: How to pass variables from a r program to mysql function?

To include the R variables called start.date and end.date, you can use paste to ...READ MORE

answered Dec 28, 2018 in Data Analytics by Tyrion anex
• 8,270 points
67 views