NA 用空格替换 [英] NA replacing with blanks

查看:57
本文介绍了NA 用空格替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用相同的方法用空格或其他字符替换 NA,但由于某种原因,这个方法不起作用.我想将数据框中的 NA 替换为空白(年份和年度列).我做错了什么?

I have used the same method for replacing NA's with blanks or other characters but for some reason this one is not working. I want to replace the NA's on my dataframe to blanks (columns year and Annual). What am I doing wrong?

shad.92 <- structure(list(year = c(1992, NA, NA, NA, NA), type = c("all age abundance index", 
"adjusted number of fish older than age 0 measured", "adjusted total number of fish measured", 
"percent YOY", "YOY abundance index"), September = c(755, 0, 
565, 100, 755), October = c(530, 0, 434, 100, 530), November = c(463, 
0, 338, 100, 463), December = c(266, 1, 136, 99.3, 264), Annual = c(2014, 
NA, NA, NA, 2012)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

我没有运气就尝试了这个:

I tried this with no luck:

shad.92 <- shad.92[is.na(shad.92)] <- ""

然后我在从年份开始的时间尝试了一个列:

I then tried one column at the time starting with year:

shad.92$year <- as.character(shad.92$year)
    shad.92$year[is.na(shad.92$year)] <- " "
    shad.92 

但我得到引号而不是空格 ("")

But I get quotation marks instead of blanks ("")

推荐答案

当您在 tibble 中有 "" 时,它会用引号显示该列的元素.但是 "" 是一个空白.当您使用基本 R 的 data.frame 时,不会打印引号.运行代码后尝试以下操作.

When you have "" in a tibble it displays the elements of that column with the quotation marks. However "" is a blank. When you use base R's data.frame the quotations are not printed. Try the below after you run your code.

as.data.frame(shad.92)

但是如果你想把它打印为 kablekable 不应该打印 "" 本身(至少不打印到终端;我没有尝试打印到 HTML).

But if you want to print this as a kable, the kable should not print the "" itself (at least not to the terminal; I didn't try printing to HTML).

library(knitr)
kable(shad.92)

这篇关于NA 用空格替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆