读取CSV并创建数据框

时间:2017-11-13 13:59:53

标签: r dataframe

我想以较短的方式保存以下命令,以便每次不重复相同的操作。

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")

1 个答案:

答案 0 :(得分:0)

正如docendo建议的那样,只需将重复的步骤概括为函数:

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")