-1

I have a data frame which looks like this

>data
Source: local data frame [10 x 10]

           CHANNEL SALES ACTUAL_SPEND ACTUAL_RETURNS SPEND_SCENARIO RETURNS_SCENARIO LB  UB Spend_Diff
1           Cinema  1324    298.56482       8.639514      298.56482         8.639514 80 120          0
2  Digital Display   924    604.29080      12.291163      604.29080        12.291163 80 120          0
3         Magazine  1164    276.62779       8.316066      276.62779         8.316066 80 120          0
4          Outdoor  1004    570.15592      11.938969      570.15592        11.938969 80 120          0
5      Paid Search  1404     82.02715       4.528442       82.02715         4.528442 80 120          0
6            Press  1084    524.82576      11.454538      524.82576        11.454538 80 120          0
7            Radio  1564    523.72771      11.442549      523.72771        11.442549 80 120          0
8   TV-Free to Air   844   1346.55335      18.347707     1346.55335        18.347707 80 120          0
9   TV-Infomercial  1484    336.86113       9.176888      336.86113         9.176888 80 120          0
10       TV-Pay TV  1244    340.21096       9.222404      340.21096         9.222404 80 120          0
Variables not shown: Returns_Diff (dbl)

I want to replace values inside this tbl_df object. I tried this

>data[1,1]<-"abc"

However it doesnt seem to work. Any help will be greatly appreciated

EDIT Adding the str of the data set and showing no change in value after assignment

 print(str(data))
        Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   10 obs. of  10 variables:
     $ CHANNEL         : Factor w/ 10 levels "Cinema","Digital Display",..: 1 2 3 4 5 6 7 8 9 10
     $ SALES           : int  1324 924 1164 1004 1404 1084 1564 844 1484 1244
     $ ACTUAL_SPEND    : num  299 604 277 570 82 ...
     $ ACTUAL_RETURNS  : num  8.64 12.29 8.32 11.94 4.53 ...
     $ SPEND_SCENARIO  : num  299 604 277 570 82 ...
     $ RETURNS_SCENARIO: num  8.64 12.29 8.32 11.94 4.53 ...
     $ LB              : num  80 80 80 80 80 80 80 80 80 80
     $ UB              : num  120 120 120 120 120 120 120 120 120 120
     $ Spend_Diff      : num  0 0 0 0 0 0 0 0 0 0
     $ Returns_Diff    : num  0 0 0 0 0 0 0 0 0 0
    NULL
    data[1,1] == "A"
    print(data)



   Source: local data frame [10 x 10]

           CHANNEL SALES ACTUAL_SPEND ACTUAL_RETURNS SPEND_SCENARIO RETURNS_SCENARIO LB  UB Spend_Diff
1           Cinema  1324    298.56482       8.639514      298.56482         8.639514 80 120          0
2  Digital Display   924    604.29080      12.291163      604.29080        12.291163 80 120          0
3         Magazine  1164    276.62779       8.316066      276.62779         8.316066 80 120          0
4          Outdoor  1004    570.15592      11.938969      570.15592        11.938969 80 120          0
5      Paid Search  1404     82.02715       4.528442       82.02715         4.528442 80 120          0
6            Press  1084    524.82576      11.454538      524.82576        11.454538 80 120          0
7            Radio  1564    523.72771      11.442549      523.72771        11.442549 80 120          0
8   TV-Free to Air   844   1346.55335      18.347707     1346.55335        18.347707 80 120          0
9   TV-Infomercial  1484    336.86113       9.176888      336.86113         9.176888 80 120          0
10       TV-Pay TV  1244    340.21096       9.222404      340.21096         9.222404 80 120          0
Variables not shown: Returns_Diff (dbl)
Rajarshi Bhadra
  • 1,826
  • 6
  • 25
  • 41

1 Answers1

3

The first column of your dataframe is a factor:

> f <- factor(c("a", "b", "c"))
> f
[1] a b c
Levels: a b c
> f[1] <- "xyz"
Warning message:
In `[<-.factor`(`*tmp*`, 1, value = "xyz") :
  invalid factor level, NA generated
> f
[1] <NA> b    c   
Levels: a b c

There must be the same warning message on your terminal. Convert the factor:

data$CHANNEL <- as.character(data$CHANNEL)

and the assignment data[1,1] <- "abc" will work.

jogo
  • 12,469
  • 11
  • 37
  • 42