简体   繁体   English

对 R 中的不同数据帧应用相同的操作

[英]Apply same operations to different data frames in R

I am trying to apply same functions to several data frames.我正在尝试将相同的功能应用于多个数据帧。 My data looks like this.我的数据看起来像这样。 I have several data frames for different years.我有几个不同年份的数据框。 I want to get rid of year specific names in order to merge data frames into one.我想摆脱特定年份的名称,以便将数据框合并为一个。

ID ID educ_1995 edu_1995 year_1995年份_1995
k1 k1 0 0 1995 1995
k2 k2 1 1 1995 1995
k3 k3 0 0 1995 1995

I have tried following:我试过以下:

files <- list(workers_1995, workers_1996)
files <- lapply(files, function)
# I tried to make a list of my data frames and then write a function for them. 


myfunc <- function(files){
  files %>% 
    rename(igang=starts_with("igang")) %>% 
    rename(year=starts_with("year")) 
}

I would like to get the results in form of same tibbles, but with changed names.我想以相同的小标题形式获得结果,但名称已更改。

Change the order and place myfunc inside the parentheses:更改顺序并将myfunc放在括号内:

files <- list(workers_1995, workers_1996)

myfunc <- function(onefile){
  onefile %>% 
    rename(igang=starts_with("igang")) %>% 
    rename(year=starts_with("year")) 
}

lapply(files, myfunc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM