Have a list of dataframe elements of IDs and sentences of the form:
(EDIT-START
The code is required to work inside a loop - so I do want explicitly to first create an empty dataframe, then populate it, then delete the content, then repopulate,..
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"
PROBLEM 1) When I populate the dataframe with
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
PROBLEM 2) Also the column names are not preserved after executing the first dataframe line
> sent.df
X.02xx. X.bc.
1 02xx bc
OBSERVATION: the following code works - although it seems inconsistent like commented
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))
Desired OUTPUT
> sent.df
ID Sentences
1 01xx ab
2 02xx bc
3 03xx cd
4 04xx de