How to merge data in R programming language?

In R language, you can use the merge() function or the cbind() function to merge data.

  1. combine
# 两个数据框的合并
merged_data <- merge(data1, data2, by = "key_column")

# 按照多个列进行合并
merged_data <- merge(data1, data2, by = c("key_column1", "key_column2"))

# 指定合并方式(默认为内连接)
merged_data <- merge(data1, data2, by = "key_column", all = TRUE) # 全连接
merged_data <- merge(data1, data2, by = "key_column", all.x = TRUE) # 左连接
merged_data <- merge(data1, data2, by = "key_column", all.y = TRUE) # 右连接
  1. merge columns from different data frames into one data frame
# 按列合并两个数据框
merged_data <- cbind(data1, data2)

The above are two common methods of merging data, the specific one to use depends on the structure and requirements of the data.

Leave a Reply 0

Your email address will not be published. Required fields are marked *