Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改data.frame中的单个值(值为新级别)?_R_Levels - Fatal编程技术网

如何更改data.frame中的单个值(值为新级别)?

如何更改data.frame中的单个值(值为新级别)?,r,levels,R,Levels,我想更改data.frame中的单个值,即NA,使用@akrun在注释中指出的代码df[307,1]: x <- factor(c("a", "b")) x[3] <- "c" Warning message: In `[<-.factor`(`*tmp*`, 3, value = "c") : invalid factor level, NA generated # one solution: x <- factor(c("a", "b")) x <- fac

我想更改data.frame中的单个值,即NA,使用@akrun在注释中指出的代码
df[307,1]:

x <- factor(c("a", "b"))
x[3] <- "c"
Warning message:
In `[<-.factor`(`*tmp*`, 3, value = "c") :
  invalid factor level, NA generated
# one solution:
x <- factor(c("a", "b"))
x <- factor(c(as.character(x), "c"))

# a second solution:
x <- factor(c("a", "b"))
levels(x) <- c("a", "b", "c")
x[3] <- "c"

x该列为
因子
类。将其更改为
字符
或在现有系数列中添加级别,然后替换
x <- factor(c("a", "b"))
x[3] <- "c"
Warning message:
In `[<-.factor`(`*tmp*`, 3, value = "c") :
  invalid factor level, NA generated
# one solution:
x <- factor(c("a", "b"))
x <- factor(c(as.character(x), "c"))

# a second solution:
x <- factor(c("a", "b"))
levels(x) <- c("a", "b", "c")
x[3] <- "c"