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

+1 vote

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, 2019 in Data Analytics by Sophie may
• 10,620 points
35,809 views

3 answers to this question.

+1 vote

Try this:

df$symbol <- as.character(df$symbol)
df$symbol[df$symbol == "DEF"] <-"NEW1"
answered Jan 11, 2019 by Tyrion anex
• 8,700 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)

Interested in a career in data analysis? Our Data Analyst Certification Course will equip you with the tools and techniques you need to succeed.

answered Aug 16, 2019 by anonymous
• 33,030 points
0 votes

Here you go, try this:

df$symbol <- as.character(df$symbol)
df$symbol[df$symbol == "ABCD.BO"] <-"Stock_T"
answered Dec 15, 2020 by Gitika
• 65,770 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 29, 2019 in Data Analytics by anonymous
• 33,030 points
17,930 views
0 votes
1 answer
+1 vote
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,798 views
+1 vote
3 answers

How to change column names of a Data frame?

Hi, To change the name of a column ...READ MORE

answered Oct 30, 2020 in Data Analytics by MD
• 95,460 points
1,706 views
+1 vote
1 answer

Parallel programming In R using GPU

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

answered Feb 4, 2019 in Data Analytics by Tyrion anex
• 8,700 points
1,391 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 26, 2019 in Data Analytics by Sophie may
• 10,620 points
6,507 views
+1 vote
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, 2019 in Data Analytics by Zulaikha
• 910 points
916 views
+1 vote
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, 2019 in Data Analytics by Tyrion anex
• 8,700 points
965 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,700 points
1,323 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP