2016-08-03 3 views
0

비슷한 질문이 이미 제기되었지만 내 특정 문제를 해결할 수 없었습니다. 나는 .R 파일 ("Mycalculus.R")에 데이터 프레임의 하위 집합에 적용해야하는 많은 기본 계산법을 포함하고 있습니다. "year"의 양식이 인수가 아닌 (yearA, yearB, yearC) 숫자가 아닌 각 연도의 하위 집합 값. 이 파일은 RDA 파일에 저장해야하는 새로운 데이터 프레임을 생성합니다. 여기하위 집합 반복, 파일 소스 및 데이터 프레임에 결과 저장

obs year income gender ageclass weight 
1  yearA 1000  F   1   10 
2  yearA 1200  M   2   25 
3  yearB 1400  M   2   5 
4  yearB 1350  M   1   11 

: 여기

id <- identif(unlist(df$year)) 
for (i in 1:length(id)){ 
    data <- subset(df, year == id[i]) 
    source ("Mycalculus.R", echo=TRUE) 
    save(content_df1,file="myresults.Rda") 
} 

메인 data.frame 안양의 정확한입니다 : 여기 코드가 for 루프 (분명히 작동하지 않는이 하나)와 같이 기대 것입니다 원본 파일 "Mycalculus.R"이하는 것입니다. "데이터"라는 데이터 프레임의 열에 수많은 기본 계산법을 적용하고 df1을 기반으로 두 개의 새 데이터 프레임 df1을 만든 다음 df2를 만듭니다. 여기 추출물 : 그래서 결국

data <- data %>% 
    group_by(gender) %>% 
    mutate(Income_gender = weighted.mean(income, weight)) 
data <- data %>% 
    group_by(ageclass) %>% 
    mutate(Income_ageclass = weighted.mean(income, weight)) 

library(GiniWegNeg) 
gini=c(Gini_RSV(data$Income_gender, weight), Gini_RSV(data$Income_ageclass,weight)) 

df1=data.frame(gini) 
colnames(df1) <- c("Income_gender","Income_ageclass") 
rownames(df1) <- c("content_df1") 

df2=(1/5)*df1$Income_gender+df2$Income_ageclass 
colnames(df2) <- c("myresult") 
rownames(df2) <- c("content_df2") 

것을, 나는 두 dataframes과 같이 얻을 :

    Income_Gender Income_Ageclass  
content_df1   ....    ....  

그리고 DF2에 대한 :

    myresult  
content_df2   ....   

하지만 DF1 저장해야하고 content_df1과 content_df2의 행 이름이 부분 집합별로 주어지면 Rda 파일로 Rf2가됩니다.

    Income_Gender Income_Ageclass  
content_df1_yearA  ....    ....  
content_df1_yearB  ....    ....  
content_df1_yearC  ....    ....  
,

    myresult 
content_df2_yearA  .... 
content_df2_yearB  ....  
content_df2_yearC  .... 

현재, 내 프로그램은 루프를 사용하지 않고 있지만 messily 작업을하고있다. 기본적으로이 코드는 2500 줄 이상의 코드입니다. (저에게 토마토를 던지지 마십시오).

누구나이 특정 요청을 도와 줄 수 있습니까? 미리 감사드립니다. 파일을 저장하지 마십시오, 또는

calcFunc <- function(df) { 
    ## Do something to the df, then return it 
    df 
} 

processFunc <- function(fname) { 
    ## Read in your table 
    x <- read.table(fname) 

    ## Do the calculation 
    x <- calcFunc(x) 

    ## Make a new file name (remember to change the file extension) 
    new_fname <- sub("something", "else", fname) 

    ## Write the .RData file 
    save(x, file = new_fname) 
} 

### Your workflow 
## Generate a vector of files 
my_files <- list.files() 

## Do the work 
res <- lapply(my_files, processFunc) 

: 당신이 당신의 단계를 기능 -이지는 경우

+0

간단하게 질문을. yearA와 yearB라는 두 개의 간단한 data.frames를 제공하고 예제 Mycalculus.R 파일에서 간단한 함수 하나만 수행하십시오.이렇게하면 다른 사람들이 귀하의 질문의 본질을 더 쉽게 이해할 수 있습니다. – JasonAizkalns

답변

2

lapply()이라는 필수 인수가 정의 된 함수로 모든 스크립트를 통합하는 것을 고려하십시오. Lapply는 마지막 df로 rowbind 할 수있는 데이터 프레임 목록을 반환합니다.

library(dplyr) 
library(GiniWegNeg) 

runIncomeCalc <- function(data, y){  
    data <- data %>% 
    group_by(gender) %>% 
    mutate(Income_gender = weighted.mean(income, weight)) 
    data <- data %>% 
    group_by(ageclass) %>% 
    mutate(Income_ageclass = weighted.mean(income, weight))  

    gini <- c(Gini_RSV(data$Income_gender, weight), Gini_RSV(data$Income_ageclass,weight)) 

    df1 <- data.frame(gini) 
    colnames(df1) <- c("Income_gender","Income_ageclass") 
    rownames(df1) <- c(paste0("content_df1_", y)) 

    return(df1) 
} 

runResultsCalc <- function(df, y){ 
    df2 <- (1/5) * df$Income_gender + df$Income_ageclass 
    colnames(df2) <- c("myresult") 
    rownames(df2) <- c(paste0("content_df2_", y) 

    return(df2) 
} 

dfIncList <- lapply(unique(df$year), function(i) {  
    yeardata <- subset(df, year == i) 
    runIncomeCalc(yeardata, i)  
}) 

dfResList <- lapply(unique(df$year), function(i) {  
    yeardata <- subset(df, year == i) 
    df <- runIncomeCalc(yeardata, i) 
    runResultsCalc(df, i)  
}) 

df1 <- do.call(rbind, dfIncList) 
df2 <- do.call(rbind, dfResList) 

이제 스크립트를 통해 소스해야하는 경우. Mycalculus.R에 runResultsCalc, runIncomeCalc을 같은 두 가지 기능을 만든 다음 다른 스크립트에서 각 전화 : 재현 예와

library(dplyr) 
library(GiniWegNeg) 

if(!exists("runIncomeCalc", mode="function")) source("Mycalculus.R") 

dfIncList <- lapply(unique(df$year), function(i) {  
    yeardata <- subset(df, year == i) 
    runIncomeCalc(yeardata, i)  
}) 

dfResList <- lapply(unique(df$year), function(i) {  
    yeardata <- subset(df, year == i) 
    df <- runIncomeCalc(yeardata, i) 
    runResultsCalc(df, i)  
}) 

df1 <- do.call(rbind, dfIncList) 
df2 <- do.call(rbind, dfResList) 
+0

당신의 솔루션은 완벽하게 작동하지만, 미적분이 하나의 데이터 프레임 (df1) 만 생성 할 때만 가능합니다. 미적분이 두 번째 데이터 프레임을 생성하면 더 이상 작동하지 않습니다 (df1 기반 df2). 여기서 코드는 더 이상 df1에 대한 결과를 생성하지 않습니다. 나는 나의 질문을 편집하여이 점에 대해 나는 분명하게 생각한다. – Elixterra

+0

동일한 fct 및 args 프로세스로 update를 참조하십시오. df1을 입력으로받는 다른 함수를 추가하기 만하면 끝에서'rbind'에'lapply'를 반복합니다. 그리고 다른 스크립트에서 두 함수를 가져와야합니다. – Parfait

+0

업데이트에서 df1 대신 runResultsCalc 부분에 df를 작성했습니다. df2 미적분은 df1의 결과를 기반으로하므로 'df2 <- (1/5) * df1 $ Income_gender + df1 $ Income_ageclass'여야합니다. 목적에 부합했는지, 아니면 나머지 코드에 미치는 영향은 무엇입니까? – Elixterra

1

당신은 다음과 같은 워크 플로를 만들 수 있습니다. processFunc에서 save 호출을 생략하고 data.frame 개체 목록을 반환하십시오. 그런 다음 data.table::rbindlist(res) 또는 do.call(rbind, list)을 사용하여 하나의 큰 data.frame 객체를 만듭니다.

+0

현재의 .R 미적분 파일 (첫 번째 단계로 funtional-ize해야 함)이 미적분의 주 data.frame을 참조하므로 하위 설정이 재정의 될 것입니다. 나는이 문제를'by '명령을 사용하려고했다. 저는 기본적으로 프로그램이 그룹에 의해 "무언가"를하도록 요구하고 있었지만 "무언가"는 주요 데이터 프레임을 가리키고있었습니다. 따라서 프로그램이 하위 집합의 수만큼 실행되지만 주 데이터 프레임을 통해 실행되는 '그룹 별'명령과 모순되었습니다. 나는 여기에 같은 문제가 있을까 봐 걱정이다. 어떻게 생각해? – Elixterra