Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R Create functions

I would like to create a function that does what in the following lines of code in one function, but to be able to choose, the time interval (15 min or 30 min or 60 min) as an argument in the function, not like in my case only after duplitates2.

#  values for V17 occur exactly 2 times, taking into account the columns V5 V1 V2)
duplitates2 <-
  Data1 %>% 
  group_by(V5, V1, V2, V17) %>%
  filter (n() == 2) %>%
  ungroup()
#Filter the data for the 30-minute time frame 
duplitates_30_2 <- filter(duplitates2, V16=='30 min')

# export a DataFrame to CSV so as to keep the names from the first row of Data1
write.csv(x = duplitates1_30_2, path = "duplitates1_30_2.csv", col_names = TRUE)

Thank you in advance for your help. I have no idea how to write it in one function.

Example data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

structure(list(V1 = c("Row", "5", "5", "5", "5", "5", "5", "5", 
"5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", 
"5", "5", "5", "5", "5", "5", "5", "5", "5"), V2 = c("Column", 
"1", "1", "3", "3", "2", "2", "1", "1", "3", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "2", "2"), V3 = c("Planes", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "3", "1", "1", "1", "2", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1"), V4 = c("Timepoint", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0"), V5 = c("Box", "62", "62", "27", "32", "10", "10", 
"14", "14", "50", "50", "43", "43", "22", "22", "48", "48", "48", 
"34", "57", "57", "60", "60", "1", "11", "42", "63", "63", "44", 
"44"), V6 = c(" No", "1", "7", "2", "29", "1", "15", "27", "28", 
"14", "15", "48", "49", "1", "2", "1", "21", "18", "19", "23", 
"24", "14", "15", "29", "31", "1", "2", "1", "36", "2"), V7 = c("X", 
"94", "104", "129", "153", "37", "60", "50", "80", "55", "92", 
"78", "555", "937", "930", "170", "72", "103", "43", "80", "231", 
"238", "81", "112", "104", "424", "438", "350", "392", "892"), 
    V8 = c("Y", "72", "103", "43", "80", "231", "238", "81", 
    "112", "104", "104", "96", "101", "92", "114", "72", "99", 
    "54", "64", "94", "104", "129", "153", "37", "60", "50", 
    "80", "55", "92", "78"), V9 = c("X2", "78", "555", "937", 
    "930", "170", "72", "96", "101", "92", "114", "72", "99", 
    "54", "78", "555", "937", "930", "170", "72", "2", "29", 
    "1", "15", "27", "28", "14", "15", "48", "49"), V10 = c("Y2", 
    "103", "43", "80", "231", "238", "81", "112", "104", "424", 
    "438", "350", "392", "892", "56", "72", "99", "54", "64", 
    "94", "233", "129", "153", "37", "60", "50", "80", "55", 
    "92", "78"), V11 = c("X3 ", "1", "1", "1", "1", "1", "1", 
    "3", "1", "72", "2", "29", "1", "15", "27", "28", "14", "15", 
    "48", "49", "96", "101", "92", "114", "72", "99", "54", "2873,91", 
    "2850,16", "-368,71"), V12 = c("Y3", "72", "2", "29", "1", 
    "15", "27", "28", "14", "15", "48", "49", "101", "92", "114", 
    "72", "99", "1", "1", "1", "1", "1", "1", "3", "1", "438", 
    "350", "392", "892", "56"), V13 = c("Dose", "10", "10", "0,1", 
    "0,1", "0,1", "0,1", "1", "1", "1", "0,1", "0,1", "10", "10", 
    "10", "10", "10", "10", "10", "1", "1", "1", "1", "1", "1", 
    "0,1", "0,1", "0,1", "0,1", "0,1"), V14 = c("Room 1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1"), V15 = c("Room 2", "2", "2", "2", "2", 
    "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
    "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
    "2"), V16 = c("Time", "30 min", "30 min", "30 min", "30 min", 
    "15 min", "15 min", "60 min", "30 min", "30 min", "15 min", 
    "60 min", "30 min", "30 min", "15 min", "30 min", "30 min", 
    "30 min", "30 min", "30 min", "30 min", "15 min", "30 min", 
    "30 min", "30 min", "30 min", "30 min", "30 min", "15 min", 
    "15 min"), V17 = c("Objects", "6", "6", "1", "13", "2", "2", 
    "12", "1", "1", "1", "11", "1", "1", "5", "1", "1", "7", 
    "1", "1", "1", "24", "1", "1", "1", "1", "14", "14", "6", 
    "6")), class = "data.frame", row.names = c(NA, -30L))

>Solution :

How about this:

library(dplyr)
myfun <- function(data, interval=c("15 min", "30 min", "60 min")){
interval <- match.arg(interval)
data %>% 
  group_by(V5, V1, V2, V17) %>%
  filter (n() == 2) %>%
  ungroup() %>% 
  filter(V16==interval)
}

duplicates_30_2 <- myfun(Data1, interval = "30 min")
duplicates_30_2
# # A tibble: 8 × 17
#   V1    V2    V3    V4    V5    V6    V7    V8    V9    V10   V11    V12   V13   V14   V15   V16   V17  
#   <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>  <chr> <chr> <chr> <chr> <chr> <chr>
# 1 5     1     1     0     62    1     94    72    78    103   1      72    10    1     2     30 m… 6    
# 2 5     1     1     0     62    7     104   103   555   43    1      2     10    1     2     30 m… 6    
# 3 5     1     1     0     48    1     170   72    555   72    28     72    10    1     2     30 m… 1    
# 4 5     1     1     0     48    21    72    99    937   99    14     99    10    1     2     30 m… 1    
# 5 5     1     1     0     57    23    80    94    72    94    49     1     1     1     2     30 m… 1    
# 6 5     1     1     0     57    24    231   104   2     233   96     1     1     1     2     30 m… 1    
# 7 5     1     1     0     63    2     438   80    14    80    54     350   0,1   1     2     30 m… 14   
# 8 5     1     1     0     63    1     350   55    15    55    2873,… 392   0,1   1     2     30 m… 14   
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading