0

有一个 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
4

3 回答 3

2
sent.df <- data.frame(ID=id1, Sentences=sent1, stringsAsFactors=FALSE)
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
于 2014-01-01T21:22:23.983 回答
1

也许你可以做

sent.df <- data.frame( 
   id=c("01xx", "02xx", "03xx", "04xx"),
   sentences=c("ab", "bc", "cd", "de")
)

如果您需要迭代工作,这将起作用

sent.df <- data.frame()
adding <- TRUE
while(adding) {
  current_id <- "next_id"
  current_sent <- "next_sent"
  sent.df <- rbind(sent.df, data.frame(id=current_id, sentences=current_sent))
  adding <- FALSE
}

但这真的很慢,应该避免。

于 2014-01-01T21:14:37.073 回答
1

如果您想轻松做到这一点,只需factor在操作前设置此选项即可避免绑定:

options(stringsAsFactors = FALSE)
于 2014-01-01T21:16:34.590 回答