首页 > 其他分享 >R语言:In `[<-.factor`(`*tmp*`, thisvar, value = " ") : invalid factor level, NA generat

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

时间:2023-01-12 10:44:23浏览次数:48  
标签:tmp sep NA header 报错 factor count3

使用命令时出现报错:

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"

标签:tmp,sep,NA,header,报错,factor,count3
From: https://www.cnblogs.com/chenwenyan/p/17045775.html

相关文章