可以在不填写NA的情况下使用rbind.fill吗? [英] Can you use rbind.fill without having it fill in NA's?

查看:231
本文介绍了可以在不填写NA的情况下使用rbind.fill吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个具有不同数量的列和列标题的数据框组合在一起.但是,在使用rbind.fill()组合它们之后,结果文件用NA填充了空白单元格.

I am trying to combine two dataframes with different number of columns and column headers. However, after I combine them using rbind.fill(), the resulting file has filled the empty cells with NA.

这非常不方便,因为其中一列包含的数据也表示为"NA"(适用于北美),因此,当我将其导入到csv中时,电子表格无法区分它们.

This is very inconvenient since one of the columns has data that is also represented as "NA" (for North America), so when I import it into a csv, the spreadsheet can't tell them apart.

我有办法吗?

  1. 使用rbind.fill函数而不用NA填充空白单元格
  1. Use the rbind.fill function without having it populate the empty cells with NA

  1. 更改列以替换NA值*

*我已经搜索了博客,并尝试了两种最受欢迎​​的解决方案:

*I've scoured the blogs, and have tried the two most popular solutions:

df$col[is.na(df$col)] <- 0, #it does not work
df$col = ifelse(is.na(df$col), "X", df$col), #it changes all the characters to numbers, and ruins the column

如果您有任何建议,请告诉我!我(很遗憾)不能分享df,但愿意回答任何问题!

Let me know if you have any advice! I (unfortunately) cannot share the df, but will be willing to answer any questions!

推荐答案

NA"NA"R不同,但是您喜欢的电子表格程序可能会将其解释为"NA".与NaN一样,NAR中的特殊值(不是数字).如果我理解正确,您的解决方案之一就是用其他方式替换代表北美的列中的"NA"值,在这种情况下,您应该可以...

NA is not the same as "NA" to R, but might be interpreted as such by your favourite spreadsheet program. NA is a special value in R just like NaN (not a number). If I understand correctly, one of your solutions is to replace the "NA" values in the column representing North America with something else, in which case you should just be able to do...

df$col[ df$col == "NA" ] <- "NorthAmerica"

这是假设您的"NA"值实际上是字符串.如果is.na()是字符串,则不会返回任何值,这就是df$col[ is.na(df$col) ] <- 0无法正常工作的原因.

This is assuming that your "NA" values are actually character strings. is.na() won't return any values if they are character strings which is why df$col[ is.na(df$col) ] <- 0 won't work.

x <- c( 1, 2, 3 , "NA" , 4 , 5 , NA )

> x[ !is.na(x) ]
[1] "1"  "2"  "3"  "NA" "4"  "5"

> x[ x == "NA" & !is.na(x) ]
[1] "NA"

解决此问题的方法

我认为您想将"NA"和任何NA保留在第一个df中,但是将由rbind.fill()形成的第二个df中的所有NA更改为类似"NotAvailable".您可以像这样完成此操作...

Method to resolve this

I think you want to leave "NA" and any NAs as they are in the first df, but make all NA in the second df formed from rbind.fill() change to something like "NotAvailable". You can accomplish this like so...

df1 <- data.frame( col = rep( "NA" , 6 ) , x = 1:6 , z = rep( 1 , 6 ) )
df2 <- data.frame( col = rep( "SA" , 2 ) , x = 1:2 , y = 5:6 )
df <- rbind.fill( df1 , df2 )
temp <- df [ (colnames(df) %in% colnames(df2)) ]
temp[ is.na( temp ) ] <- "NotAvailable"
res <- cbind( temp , df[ !( colnames(df) %in% colnames(df2) ) ] )

#df has real NA values in column z and column y. We just want to get rid of y's
df

#     col x  z  y
#   1  NA 1  1 NA
#   2  NA 2  1 NA
#   3  NA 3  1 NA
#   4  NA 4  1 NA
#   5  NA 5  1 NA
#   6  NA 6  1 NA
#   7  SA 1 NA  5
#   8  SA 2 NA  6

#res has "NA" strings in col representing "North America" and NA values in z, whilst those in y have been removed
#More generally, any NA in df1 will be left 'as-is', whilst NA from df2 formed using rbind.fill will be converted to character string "NotAvilable"
res

#     col x            y  z
#   1  NA 1 NotAvailable  1
#   2  NA 2 NotAvailable  1
#   3  NA 3 NotAvailable  1
#   4  NA 4 NotAvailable  1
#   5  NA 5 NotAvailable  1
#   6  NA 6 NotAvailable  1
#   7  SA 1            5 NA
#   8  SA 2            6 NA

这篇关于可以在不填写NA的情况下使用rbind.fill吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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