0

I am learning R programming and have imported the CSV file. Now, I would like to change the variable value for symbol column. If the value is "ABCD.BO" then "Stock_T". If the value is "XYZ.BO" then "Stock_Y". 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 == "ABCD.BO"] <- "Stock_T"
df$symbol[df$symbol == "XYZ.BO"] <- "Stock_Y"

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

symbol  Date    Adj.Close
ABCD.BO 9/21/2011   201.33
ABCD.BO 9/22/2011   192.9
ABCD.BO 9/23/2011   190.47
ABCD.BO 9/26/2011   185.95
ABCD.BO 9/27/2011   190.52
ABCD.BO 9/28/2011   191.82
ABCD.BO 9/29/2011   189.49
ABCD.BO 9/30/2011   186.74
XYZ.BO  2/10/2012   334.11
XYZ.BO  2/13/2012   336.14
XYZ.BO  2/14/2012   343.74
XYZ.BO  2/15/2012   351.83
XYZ.BO  2/16/2012   352.8
XYZ.BO  2/17/2012   350
XYZ.BO  2/20/2012   350
XYZ.BO  2/21/2012   354.63
XYZ.BO  2/22/2012   333.68
XYZ.BO  2/23/2012   340.57
XYZ.BO  2/24/2012   327.9
Surya
  • 15,703
  • 3
  • 51
  • 74
user3762120
  • 256
  • 2
  • 12

3 Answers3

2

here you go, try this:

df$symbol <- as.character(df$symbol)
df$symbol[df$symbol == "ABCD.BO"] <-"Stock_T"
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
parvij
  • 1,381
  • 3
  • 15
  • 31
1

There are a number of ways to do this. Here are two.

The first is for changing all the factor levels. If you have a vector of symbols to change to, and they're in the same order of the levels of the symbols, it's probably easiest to do

within(df, levels(symbol) <- paste("Stock", c("T", "Y"), sep = "_"))

The second way is the "manual" way to do it, and can be used if you only want to change a couple of the factor levels. In the code below, the line symbol <- as.factor(symbol) can be removed and the first column will be returned as character class. Leaving it in will factor the first column after the change.

within(df, {
    symbol <- as.character(symbol)
    symbol[symbol == "ABCD.BO"] <- "Stock_T"
    symbol[symbol == "XYZ.BO"] <- "Stock_Y"
    symbol <- as.factor(symbol) 
})

Note that you don't necessarily need within here. I like it because it returns the changed data frame.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
  • I would suggest being more explicit with `levels`, providing a named list, perhaps something like: `levels(df$symbol) <- list(Stock_T = "ABCD.BO", Styck_Y = "XYZ.BO")` – A5C1D2H2I1M1N2O1R2T1 Sep 22 '14 at 04:03
1

It is not changing because the new value is not one of the named factors of that column. You can either use:

stringsAsFactors = FALSE in read.csv command

or convert symbol column to character:

df$symbol = as.character(df$column)

Then your commands will work.

rnso
  • 23,686
  • 25
  • 112
  • 234