matching 3 dataframes with different number of rows

I have 3 dataframes with different number of rows available on this link
https://drive.google.com/drive/folders/1vPno03j3YtcAARgKssibAcUX177GmF_r?usp=sharing
What I want is the like this Untitled spreadsheet - Google Sheets

I want those rows in conf11_pos where particlei and particlej of conf11_particles are present and paste their values in conf11_particles columns. Process should be repititive so I have used a for loop.
What I have tried till now.

shear2_ft <- read_csv("~/Documents/For_Shah/3D/ShearTest-2/2-ShearTest_DEM/sheartest-2_ft_fn.csv") %>% na.omit()  %>% filter(type ==0)  %>% select(-c(isub,jsub))


spheres <- read_csv("~/Documents/For_Shah/3D/ShearTest-2/2-ShearTest_DEM/sheartest-2_spheres.csv") %>% na.omit()  %>% select(-c(group)) %>% select(-c(shapeName))

conf_filing <-  data.frame(conf=c("conf11","conf12"))

pos <- function(spheres,shear2_ft,conf_filing){
  
  conf11_pos <-  spheres %>% filter(V1 == c(conf_filing$conf[1])) %>%  select(V1,cluster,position.x,position.y,position.z)
  conf12_pos <-  spheres  %>% filter(V1 == c(conf_filing$conf[2])) %>% select(V1,cluster,position.x,position.y,position.z)
  
  conf11_particles <- shear2_ft %>% filter(V1 == c(conf_filing$conf[1])) %>% select(1:11)
  conf12_particles <- shear2_ft %>% filter(V1 == c(conf_filing$conf[2])) %>% select(1:11)
  
  dx<- conf12_pos$position.x-conf11_pos$position.x
  dy <- conf12_pos$position.y-conf11_pos$position.y
  dz <- conf12_pos$position.z-conf11_pos$position.z
  x <- cbind(conf11_pos$cluster,dx,dy,dz) %>% data.frame() 
  x <- x %>% arrange(V1)
  
  for(i in seq(conf11_particles)){
    
    if (conf11_particles[i,2]==conf11_pos$cluster[i]){
      
      conf11_particles[i,4]  <- paste(conf11_pos$position.x[i])
      conf11_particles[i,5]  <- paste(conf11_pos$position.y[i])
      conf11_particles[i,6]  <- paste(conf11_pos$position.z[i])
      
       if (conf11_particles[i,3]==conf11_pos$cluster[i]){
      
      conf11_particles[i,7]  <- paste(conf11_pos$position.x[i])
      conf11_particles[i,8]  <- paste(conf11_pos$position.y[i])
      conf11_particles[i,9]  <- paste(conf11_pos$position.z[i])
      
      
      }
    }
  }

I don't fully understand what the exact operations you're trying to do are, but seems you can fully avoid the for loop using match() as in your title, or left_join().

library(dplyr)

conf11_pos <- read.csv("files/conf11_pos.csv")

conf11_particules <- read.csv("files/conf11_particles.csv")


conf11_particules |>
  left_join(conf11_pos,
            by = c(Particlei = "cluster")) |>
  left_join(conf11_pos,
            by = c(Particlej = "cluster"))

There are more explanations in corresponding chapter of r4ds.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.