1

I would like to use R to merge some files based on the values of some parameters. I am using the following csv file

ls table.csv  
filename,type,rep,category,param
file1,1,4,A,1
file2,3,1,B,1
file3,2,1,A,2
file4,1,1,C,3
file5,1,1,C,2
file6,2,2,D,1
file7,3,1,C,2
file8,3,1,B,3
file9,3,1,B,3
file10,1,4,A,1
file11,1,1,B,1

ta <- readr::read_csv("table.csv")
Parsed with column specification:
cols(
filename = col_character(),
type = col_integer(),
rep = col_integer(),
category = col_character(),
param = col_integer()
)

I would like to merge the files that have identical values for ta$type, ta$rep and ta$category (ta$param is irrelevant).

So I would merge: file1, file10 [1,4,A] file2, file8, file9 [3,1,B] file4, file5 [1,1,C] file3 [2,1,A], file6 [2,2,D], file7 [3,1,C] and file11[1,1,B] will not be merged with any other files.

Does anyone has any ideas how to do this ? Thanks !

6
  • What is your expected output?
    – akrun
    Jun 13, 2018 at 15:17
  • Three new concatenated files (file1+file10, file2+file8+file9 and file4+file5)
    – marie
    Jun 13, 2018 at 15:22
  • Not clear from the comments
    – akrun
    Jun 13, 2018 at 15:22
  • I would like to end up with three output files, newfile1 <- rbind(file1, file10), newfile2 <- rbind(file2, file8, file9) ...etc
    – marie
    Jun 13, 2018 at 15:48
  • Your question would greatly benefit from some additional formatting where you are referring to code or data constructs, such as: fileX [a, b, c]. It will make it easier for people to understand your question and help. Jun 13, 2018 at 17:06

1 Answer 1

0

An option is to use dplyr::group_by on columns (type, rep, category) and then summarise using paste0 with collapse="+" argument. The solution will be as:

library(readr)

library(dplyr)

ta <- readr::read_csv("table.csv")

ta %>% group_by(type, rep, category) %>%
  summarise(file = paste0(filename, collapse="+"))


# # A tibble: 7 x 4
# # Groups: type, rep [?]
#    type   rep category file             
#   <int> <int> <chr>    <chr>            
# 1     1     1 B        file11           
# 2     1     1 C        file4+file5      
# 3     1     4 A        file1+file10     
# 4     2     1 A        file3            
# 5     2     2 D        file6            
# 6     3     1 B        file2+file8+file9
# 7     3     1 C        file7
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.