0

I would like to save the following commands in a shorter way so that each time I do not repeat the same actions.

aa <- read_excel("C:/Users/Piotr/Desktop/aa.xlsx")
bb <- read_excel("C:/Users/Piotr/Desktop/bb.xlsx")

data=as.data.frame(aa[1:100,c(1, 18, 20, 22, 35, 39, 41, 44)])
row.names(data) <- data$Player
data=data[,-1]

data2=as.data.frame(bb[1:100,c(1, 18, 20, 22, 35, 39, 41, 44)])
row.names(data2) <- data2$Player
data2=data2[,-1]

data_sets <- c("data","data2")
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Kim
  • 117
  • 2
  • 11

1 Answers1

0

As docendo suggested, just generalize your repeated steps into a function:

clean_data <- function(x) {

data <- read_excel(paste0("C:/Users/Piotr/Desktop/", x, ".xlsx"))
data=as.data.frame(data[1:100,c(1, 18, 20, 22, 35, 39, 41, 44)])
  row.names(data) <- data$Player
  data=data[,-1]
return(data)
}

data_sets = c(clean_data("aa"), clean_data("bb"))
names(data_sets) = c("data", "data2")
David Klotz
  • 2,401
  • 1
  • 7
  • 16
  • In my above example the result is this: > data_sets=c("data","data2") > data_sets [1] "data" "data2" – Kim Nov 13 '17 at 14:41