база R
set2[setdiff(names(set),names(set2))] <- NA
rbind(set2,set)
# text id line_number content_type section
# 1 Sample Report <NA> <NA> <NA> <NA>
# 2 this is a sample one 1 paragraph introduction
# 3 first batch is: two 2 paragraph content
# 4 second batch is: three 3 paragraph summary
или для одного вкладыша, который не изменяется set2
:
rbind('[<-'(set2,setdiff(names(set),names(set2)),value= NA),set)
dplyr
dplyr::bind_rows(set2,set)
# text id line_number content_type section
# 1 Sample Report <NA> <NA> <NA> <NA>
# 2 this is a sample one 1 paragraph introduction
# 3 first batch is: two 2 paragraph content
# 4 second batch is: three 3 paragraph summary
data.table
data.table::rbindlist(list(set2,set),fill=TRUE)
# text id line_number content_type section
# 1: Sample Report NA NA NA NA
# 2: this is a sample one 1 paragraph introduction
# 3: first batch is: two 2 paragraph content
# 4: second batch is: three 3 paragraph summary
примечание по порядку столбцов
Порядок столбцов определяетсяпервый data.frame
, поэтому столбец text
был перемещен влево.Добавьте [names(set)]
к любому ответу, чтобы вернуть исходный заказ.
data
set <- data.frame("id"=c("one", "two","three"), "line_number"=c("1", "2", "3"),
"content_type"=c("paragraph", "paragraph","paragraph"),
"text"=c("this is a sample","first batch is:", "second batch is:"),
"section"=c("introduction","content","summary"))
set2 <- data.frame(text ="Sample Report")