D Kincaid
D Kincaid

Reputation: 281

How do I write a function to manipulate several dataframes the same way?

Complete novice. I do not know how to write a function. I have several dataframes that all need to be manipulated in the same way and the output should be dataframes with the same names. I have functioning code that can manipulate a single dataframe. I would like to be able to manipulate several at once.

Here are 2 example df's:

ex1 <- structure(list(info1 = c("Day", "2018.04.03  10:47:33", "2018.04.03  11:20:04", "2018.04.03  11:35:04"), info2 = c("Status_0", "Ok", "Ok", "Ok"
), X = c(200L, 1L, 2L, 3L), X.1 = c(202.5, 1, 2, 3), X.2 = c(205L, 
1L, 2L, 3L), X.3 = c(207.5, 1, 2, 3), X.4 = c(210L, 1L, 2L, 3L
), X.5 = c(212.5, 1, 2, 3), X.6 = c(215L, 1L, 2L, 3L)), class = "data.frame", row.names = c(NA, -4L))

ex2 <- structure(list(info1 = c("Day", "2018.04.10  12:47:33", "2018.04.10  13:20:04", "2018.04.10  13:35:04"), info2 = c("Status_0", "Ok", "Ok", "Ok"
), X = c(200L, 1L, 2L, 3L), X.1 = c(202.5, 1, 2, 3), X.2 = c(205L, 
1L, 2L, 3L), X.3 = c(207.5, 1, 2, 3), X.4 = c(210L, 1L, 2L, 3L
), X.5 = c(212.5, 1, 2, 3), X.6 = c(215L, 1L, 2L, 3L)), class = "data.frame", row.names = c(NA, -4L))

Here is the functioning code to manipulate 'ex1'

library(tidyverse)
library(lubridate)

  colnames(ex1) <- ex1[1,]                            
  ex1 <- ex1 %>% 
    slice(-1) %>%                                                               
    rename(Date.Time = "Date/Time") %>% 
    mutate(timestamp = parse_date_time(Date.Time, "%Y.%m.%d %H:%M:%S")) %>%     
    select(timestamp, Date.Time, everything()) %>% select(-Date.Time) %>%       
    select(-c(Status_0:"202.5", "212.5":"215"))
  colnames(ex1)[-1] <- paste("raw", colnames(ex1)[-1], sep = "_")

Secondary question: let's say I wanted to change the function so it accepted a df, but also a type (i.e., raw or comp) and the function input would be tidydatafunc(df, type). If I input type=comp it would change the last line of the code where I have "raw" to "comp". How could I change the function to accomodate this?

Any help is greatly appreciated. I'm sure this is basic stuff for most of you!

Upvotes: 1

Views: 63

Answers (1)

Brandon Bertelsen
Brandon Bertelsen

Reputation: 44638

Wrap your script in function and specify params.

my_fun <- function(df, type = 'comp') {
  # basic input validation is extremely useful
  stopifnot(is.data.frame(df))
  stopifnot(is.character(type))

  colnames(df) <- df[1,]                            
  ex1 <- df %>% 
  slice(-1) %>%                                                               
  rename(Date.Time = "Date/Time") %>% 
  mutate(timestamp = parse_date_time(Date.Time, "%Y.%m.%d %H:%M:%S")) %>%     
  select(timestamp, Date.Time, everything()) %>% select(-Date.Time) %>%       
  select(-c(Status_0:"202.5", "212.5":"215"))
  # pass the character type
  colnames(df)[-1] <- paste(type, colnames(df)[-1], sep = "_")
  return(df)
}

Then you can use it.

my_fun(ex1, "comp") # view
new_ex1 <- my_fun(ex1, "comp") # save to variable new_ex1

Upvotes: 2

Related Questions