我有一个看起来像这样的数据框
>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)
我想替换此tbl_df对象中的值。我试过这个
>data[1,1]<-"abc"
然而它似乎没有用。任何帮助将不胜感激
修改 添加数据集的str并在赋值后不显示值
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)
答案 0 :(得分:3)
数据框的第一列是一个因素:
> 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
终端上必须有相同的警告信息。转换因子:
data$CHANNEL <- as.character(data$CHANNEL)
,作业data[1,1] <- "abc"
将有效。