df <- read.csv("/Users/imac/Desktop/left_join/blast_lineage.csv") df <- subset(df, virus_name_N == "Viruses" | virus_name_X == "Viruses") df[is.na(df)] <- "NA" write.csv(df, "/Users/imac/Desktop/left_join/blast_lineage_new.csv", row.names = FALSE)为什么输出了空表
时间: 2024-03-04 20:54:00 浏览: 182
v-blast.rar_V BLAST _blast_v-blast
可能是因为在使用`df[is.na(df)] <- "NA"`替换空缺值时,将整个数据框中所有的NA值都替换成了"NA"字符串,导致数据框中所有的值都变成了字符型,而原本是数值型的列也被转换成了字符型,这可能导致数据被保存时出现了问题。
你可以尝试修改代码,在替换空缺值时只针对需要替换的列进行替换,例如:
```R
df$column_name_N[is.na(df$column_name_N)] <- "NA"
df$column_name_X[is.na(df$column_name_X)] <- "NA"
```
其中,`column_name_N`和`column_name_X`分别为需要替换空缺值的列名,修改后的代码如下:
```R
df <- read.csv("/Users/imac/Desktop/left_join/blast_lineage.csv")
df <- subset(df, virus_name_N == "Viruses" | virus_name_X == "Viruses")
df$column_name_N[is.na(df$column_name_N)] <- "NA"
df$column_name_X[is.na(df$column_name_X)] <- "NA"
write.csv(df, "/Users/imac/Desktop/left_join/blast_lineage_new.csv", row.names = FALSE)
```
这样,你就可以得到正确的输出结果了。
阅读全文