簡體   English   中英

對 R 中的不同數據幀應用相同的操作

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

我正在嘗試將相同的功能應用於多個數據幀。 我的數據看起來像這樣。 我有幾個不同年份的數據框。 我想擺脫特定年份的名稱,以便將數據框合並為一個。

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

我試過以下:

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

我想以相同的小標題形式獲得結果,但名稱已更改。

更改順序並將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