我想要一个空的数据框,然后添加行值。我创建数据框的方式如下:
result_df <- data.frame("Hospital" = character(), "State" = character(), stringsAsFactors = FALSE)
然后我添加第一行:
result_df <- rbind(result_df, list("D W MCMILLAN MEMORIAL HOSPITAL", "AL"))
正如额外信息一样,我向您展示了以下命令的结果:
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
然后我将下一行添加到数据框
result_df <- rbind(result_df, list("ARKANSAS METHODIST MEDICAL CENTER", "TX"))
这就是我得到的:
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
当我输入result_df以查看数据框的内容时,结果如下:
X.D.W.MCMILLAN.MEMORIAL.HOSPITAL. X.AL.
1 D W MCMILLAN MEMORIAL HOSPITAL AL
2 <NA> <NA>
我想这可以使用stringAsFactors = FALSE解决,有没有人对这个问题有所了解?
答案 0 :(得分:0)
rbind
函数需要具有相同的列名。如果使用相同的列名创建数据框,则可以组合这些数据框而不使用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))
这是最终输出。
print(result_df)
Hospital state
1 D W MCMILLAN MEMORIAL HOSPITAL AL
2 ARKANSAS METHODIST MEDICAL CENTER TX
答案 1 :(得分:0)
我们可以使用rbindlist
data.table
library(data.table)
rbindlist(list(result_df, list("D W MCMILLAN MEMORIAL HOSPITAL", "AL")))
# Hospital State
#1: D W MCMILLAN MEMORIAL HOSPITAL AL