有一个 ID 的数据框元素列表和以下形式的句子:
(EDIT-START 代码需要在循环内工作 - 所以我确实想明确地首先创建一个空数据框,然后填充它,然后删除内容,然后重新填充,.. EDIT-END)
# creating an empty dataframe
sent.df <- data.frame(ID=character(), Sentences=character())
# have IDs are like:
id1 <- "01xx"
id2 <- "02xx"
id3 <- "03xx"
id4 <- "04xx"
# have sentences are like:
sent1 <- "ab"
sent2 <- "bc"
sent3 <- "cd"
sent4 <- "de"
问题1)当我用
sent.df <- rbind(sent.df, c(id1, sent1))
sent.df <- rbind(sent.df, c(id2, sent2)) #*
sent.df <- rbind(sent.df, c(id3, sent3))
sent.df <- rbind(sent.df, c(id4, sent4))
#I get this unexplicable errors, after the second command marked with "#*"
s:
1: In `[<-.factor`(`*tmp*`, ri, value = "03xx") :
invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ri, value = "cd") :
invalid factor level, NA generated
问题 2)在执行第一个数据帧行后,也不会保留列名
> sent.df
X.02xx. X.bc.
1 02xx bc
观察:以下代码有效 - 尽管它看起来像评论的不一致
sent.df <- data.frame(ID=numeric(), Sentences=numeric()) # inconsistent class initialization
sent.df[1,] <- c("01xx", "ab") # rbind doesn't work. see above.
sent.df <- rbind(sent.df, c(id2, sent2))
sent.df <- rbind(sent.df, c(id3, sent3))
sent.df <- rbind(sent.df, c(id4, sent4))
所需的输出
> sent.df
ID Sentences
1 01xx ab
2 02xx bc
3 03xx cd
4 04xx de