简体   繁体   English

在数据帧R中插入NA值

[英]Insert NA values in a data frame R

I want an empty data frame and later add row values to it. 我想要一个空的数据框,然后添加行值。 The way I create a data frame is the following: 我创建数据框的方式如下:

result_df <- data.frame("Hospital" = character(), "State" = character(), stringsAsFactors = FALSE)

Then I add the first row: 然后我添加第一行:

result_df <- rbind(result_df, list("D W MCMILLAN MEMORIAL HOSPITAL", "AL"))

Just as extra information I show you the result of the following command: 就像额外信息一样,我向您展示以下命令的结果:

str(result_df)

'data.frame':   1 obs. of  2 variables:
 $ X.D.W.MCMILLAN.MEMORIAL.HOSPITAL.: Factor w/ 1 level "D W MCMILLAN MEMORIAL HOSPITAL": 1
 $ X.AL.                            : Factor w/ 1 level "AL": 1

Then I add the next row to the data frame 然后我将下一行添加到数据框

result_df <- rbind(result_df, list("ARKANSAS METHODIST MEDICAL CENTER", "TX"))

and this is what I get: 这就是我得到的:

Warning messages:
1: In `[<-.factor`(`*tmp*`, ri, value = "ARKANSAS METHODIST MEDICAL CENTER") :
  invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ri, value = "TX") :
  invalid factor level, NA generated

When I type result_df to see the content of the data frame this is the result: 当我输入result_df以查看数据框的内容时,结果如下:

  X.D.W.MCMILLAN.MEMORIAL.HOSPITAL. X.AL.
1    D W MCMILLAN MEMORIAL HOSPITAL    AL
2                              <NA>  <NA>

I guess this could be solved using stringAsFactors = FALSE, does any one have an idea about this problem? 我想这可以使用stringAsFactors = FALSE解决,有没有人对这个问题有所了解?

The rbind function needs to have the same column names. rbind函数需要具有相同的列名。 If you created the data frame with the same column names, you can combine these data frames without NA. 如果使用相同的列名创建数据框,则可以组合这些数据框而不使用NA。

result_df <- rbind(result_df, data.frame(Hospital = "D W MCMILLAN MEMORIAL HOSPITAL", 
                                         state = "AL",
                                         stringsAsFactors = FALSE))

result_df <- rbind(result_df, data.frame(Hospital = "ARKANSAS METHODIST MEDICAL CENTER", 
                                         state = "TX",
                                         stringsAsFactors = FALSE)) 

Here is the final output. 这是最终输出。

print(result_df)
                           Hospital state
1    D W MCMILLAN MEMORIAL HOSPITAL    AL
2 ARKANSAS METHODIST MEDICAL CENTER    TX

We can use rbindlist from data.table 我们可以使用rbindlistdata.table

library(data.table)
rbindlist(list(result_df, list("D W MCMILLAN MEMORIAL HOSPITAL", "AL")))
#                         Hospital State
#1: D W MCMILLAN MEMORIAL HOSPITAL    AL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM