R语言:In `[<-.factor`(`*tmp*`, thisvar, value = " ") : invalid factor level, NA generated报错

使用命令时出现报错:

count3 = read.table("summary.txt",header=T,sep="\t")
count3[is.na(count3)] <- "rs111"

In `[<-.factor`(`*tmp*`, thisvar, value = "rs111") :
  invalid factor level, NA generated

报错原因

"rs111"是character,而我要替换的NA属性为factor,factor属性的NA不能替换为character属性的"rs111"

解决办法

加上stringsAsFactors = FALSE参数:

count3 = read.table("summary.txt",header=T,sep="\t", stringsAsFactors = FALSE)
count3[is.na(count3)] <- "rs111"
posted @ 2023-01-12 10:37  橙子牛奶糖  阅读(250)  评论(0编辑  收藏  举报