R 데이터프레임 행 추가

1 개요[ | ]

How to add a row to a data frame in R
R 데이터프레임 행 추가

2 방법1: df[] ★[ | ]

df <- data.frame(
  Class = c("A", "A", "B"),
  Name  = c("Alice", "Bob", "Carol"),
  Math  = c(90, 80, NA),
  stringsAsFactors = FALSE
)
df[nrow(df)+1,] <- c("B", "Dave", 70)
df

3 방법2: rbind()[ | ]

df <- data.frame(
  Class = c("A", "A", "B"),
  Name  = c("Alice", "Bob", "Carol"),
  Math  = c(90, 80, NA),
  stringsAsFactors = FALSE
)
row <- data.frame("B", "Dave", 70)
names(row) <- c("Class", "Name", "Math")
df <- rbind(df, row)
df
df <- data.frame(
  Class = c("A", "A", "B"),
  Name  = c("Alice", "Bob", "Carol"),
  Math  = c(90, 80, NA),
  stringsAsFactors = FALSE
)
row <- data.frame(
  Class = c("B"),
  Name  = c("Dave"),
  Math  = c(70),
  stringsAsFactors = FALSE
)
df <- rbind(df, row)
df

4 방법3: add_row()[ | ]

df <- data.frame(
  Class = c("A" ,"A", "B"),
  Name  = c("Alice", "Bob", "Carol"),
  Math  = c(90, 80, NA),
  stringsAsFactors = FALSE
)
library(tibble)
df <- df %>% add_row(Class="B", Name="Dave", Math=70)
df

5 같이 보기[ | ]

6 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}