📜  R中按列名合并数据帧

📅  最后修改于: 2022-05-13 01:55:14.528000             🧑  作者: Mango

R中按列名合并数据帧

在本文中,我们将看到如何使用 R 编程语言中的合并按列名称合并数据帧。

基础 R 中的 merge()函数可用于按公共列或行名称合并输入数据帧。 merge()函数保留数据帧的所有行名称,其行为类似于内部联接。数据帧按照在输入函数调用中出现的顺序进行组合。

示例 1:按列合并两个数据框

可以使用两个数据帧中的公共列将两个数据帧合并在一起。用于合并的列可以在函数调用期间在“by”参数中指定。输出数据帧生成的行与“by”参数中指定的列中遇到的常见条目等效。



R
# creating first dataframe
df1 <- data.frame(col1 = LETTERS[1:6],
                  col2a = c(5:10),
                  col3a = TRUE)
  
print ("First DataFrame")
print (df1)
df2 <- data.frame(col1 = LETTERS[4:8],
                  col2b= c(4:8),
                  col3b = FALSE)
  
print ("Second DataFrame")
print (df2)
df_merge <- merge(df1,df2,by="col1")
  
print("Merged DataFrame")
print (df_merge)


R
# creating first dataframe
df1 <- data.frame(col1 = LETTERS[1:6],
                  col2a = c(5:10),
                  col3a = TRUE)
  
print ("First DataFrame")
print (df1)
df2 <- data.frame(col1 = LETTERS[4:8],
                  col2b= c(4:8),
                  col3b = FALSE)
  
print ("Second DataFrame")
print (df2)
df_merge <- merge(df1, df2, by = "col1",
                  all.x = TRUE)
  
print("Merged DataFrame")
print (df_merge)


R
# creating first dataframe
df1 <- data.frame(col1 = LETTERS[1:6],
                  col2a = c(5:10),
                  col3a = TRUE)
  
print ("First DataFrame")
print (df1)
df2 <- data.frame(col1 = LETTERS[4:8],
                  col2b= c(4:8),
                  col3b = FALSE)
  
print ("Second DataFrame")
print (df2)
df_merge <- merge(df1, df2, by = "col1", 
                  all.y = TRUE)
  
print("Merged DataFrame")
print (df_merge)


R
# creating first dataframe
df1 <- data.frame(col1 = LETTERS[1:6],
                  col2a = c(5:10),
                  col3a = TRUE)
print ("First DataFrame")
print (df1)
df2 <- data.frame(col1 = LETTERS[4:8],
                  col2b= c(4:8),
                  col3b = FALSE)
print ("Second DataFrame")
print (df2)
df_merge <- merge(df1,df2,by="col1",all.x = TRUE, all.y=TRUE)
  
print("Merged DataFrame")
print (df_merge)


R
# creating first dataframe
df1 <- data.frame(col1 = LETTERS[1:6],
                  col2a = c(5:10),
                  col3a = TRUE)
print ("First DataFrame")
print (df1)
df2 <- data.frame(col1 = LETTERS[4:8],
                  col2b= c(4:8),
                  col3b = FALSE)
print ("Second DataFrame")
print (df2)
df3 <- data.frame(col1 = LETTERS[7:9],
                  col2c= c(2:4),
                  col3c = NA)
print ("Third DataFrame")
print (df3)
df_merge12 <- merge(df1,df2,by="col1",all.x=TRUE, all.y = TRUE)
df_merge <- merge(df_merge12,df3,by="col1",all.x=TRUE, all.y = TRUE)
  
print("Merged DataFrame")
print (df_merge)


输出:

示例 2:合并具有缺失值的数据框

为了保留第一个数据帧的所有值,无论它们在“by”参数中是否具有共同值,我们设置 all.x = true。属于第二个数据帧的缺失值附加有 NA 值。

电阻

# creating first dataframe
df1 <- data.frame(col1 = LETTERS[1:6],
                  col2a = c(5:10),
                  col3a = TRUE)
  
print ("First DataFrame")
print (df1)
df2 <- data.frame(col1 = LETTERS[4:8],
                  col2b= c(4:8),
                  col3b = FALSE)
  
print ("Second DataFrame")
print (df2)
df_merge <- merge(df1, df2, by = "col1",
                  all.x = TRUE)
  
print("Merged DataFrame")
print (df_merge)

输出:

为了保留第二个数据帧的所有值,无论它们在“by”参数中是否具有共同值,我们设置 all.y = true。属于第一个数据框列的缺失值附加有 NA 值。



电阻

# creating first dataframe
df1 <- data.frame(col1 = LETTERS[1:6],
                  col2a = c(5:10),
                  col3a = TRUE)
  
print ("First DataFrame")
print (df1)
df2 <- data.frame(col1 = LETTERS[4:8],
                  col2b= c(4:8),
                  col3b = FALSE)
  
print ("Second DataFrame")
print (df2)
df_merge <- merge(df1, df2, by = "col1", 
                  all.y = TRUE)
  
print("Merged DataFrame")
print (df_merge)

输出:

以下代码说明了需要保留两个输入数据帧的所有行时的用法。

电阻

# creating first dataframe
df1 <- data.frame(col1 = LETTERS[1:6],
                  col2a = c(5:10),
                  col3a = TRUE)
print ("First DataFrame")
print (df1)
df2 <- data.frame(col1 = LETTERS[4:8],
                  col2b= c(4:8),
                  col3b = FALSE)
print ("Second DataFrame")
print (df2)
df_merge <- merge(df1,df2,by="col1",all.x = TRUE, all.y=TRUE)
  
print("Merged DataFrame")
print (df_merge)

输出:

示例 3:合并两个以上的数据帧

也可以合并两个以上的数据帧。但是,数据帧是使用 merge() 方法调用合并的,按照它们在函数调用中出现的顺序一次两个。因此,如果要合并 n 个数据帧,则需要 n-1 个函数调用。

电阻

# creating first dataframe
df1 <- data.frame(col1 = LETTERS[1:6],
                  col2a = c(5:10),
                  col3a = TRUE)
print ("First DataFrame")
print (df1)
df2 <- data.frame(col1 = LETTERS[4:8],
                  col2b= c(4:8),
                  col3b = FALSE)
print ("Second DataFrame")
print (df2)
df3 <- data.frame(col1 = LETTERS[7:9],
                  col2c= c(2:4),
                  col3c = NA)
print ("Third DataFrame")
print (df3)
df_merge12 <- merge(df1,df2,by="col1",all.x=TRUE, all.y = TRUE)
df_merge <- merge(df_merge12,df3,by="col1",all.x=TRUE, all.y = TRUE)
  
print("Merged DataFrame")
print (df_merge)

输出: