简体   繁体   中英

How to integrate two halves of a symmetrical data frame

I have two data frames, they are halves of the whole matrix:

library(tidyverse)
df1 <- structure(list(`Cluster1-CONTROL` = c(568L, NA, NA), `Cluster1-DAY03` = c(
  578L,
  686L, NA
), `Cluster1-DAY06` = c(735L, 840L, 1059L)), row.names = c(
  "Cluster1-CONTROL",
  "Cluster1-DAY03", "Cluster1-DAY06"
), class = "data.frame")

df1
#>                  Cluster1-CONTROL Cluster1-DAY03 Cluster1-DAY06
#> Cluster1-CONTROL              568            578            735
#> Cluster1-DAY03                 NA            686            840
#> Cluster1-DAY06                 NA             NA           1059

df2 <- structure(list(`Cluster1-CONTROL` = c(568L, NA, NA), `Cluster1-DAY03` = c(
  639L,
  686L, NA
), `Cluster1-DAY06` = c(813L, 861L, 1059L)), row.names = c(
  "Cluster1-CONTROL",
  "Cluster1-DAY03", "Cluster1-DAY06"
), class = "data.frame")


df2
#>                  Cluster1-CONTROL Cluster1-DAY03 Cluster1-DAY06
#> Cluster1-CONTROL              568            639            813
#> Cluster1-DAY03                 NA            686            861
#> Cluster1-DAY06                 NA             NA           1059

What I want to do is to create this final data frame where the df2 will be flipped and put at the bottom of df1 :

                 Cluster1-CONTROL Cluster1-DAY03 Cluster1-DAY06
Cluster1-CONTROL              568            578            735
Cluster1-DAY03                639            686            840
Cluster1-DAY06                813            861           1059

If they are exactly symmetrical, you can first copy df1 dataframe into another object, take upper triangular part of df2 and put it on lower triangular part of the new object.

df3 <-df1
df3[lower.tri(df3)] <- df2[upper.tri(df2)]

df3
#                 Cluster1-CONTROL Cluster1-DAY03 Cluster1-DAY06
#Cluster1-CONTROL              568            578            735
#Cluster1-DAY03                639            686            840
#Cluster1-DAY06                813            861           1059

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM