비슷한 질문이 이미 제기되었지만 내 특정 문제를 해결할 수 없었습니다. 나는 .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)
: 당신이 당신의 단계를 기능 -이지는 경우
간단하게 질문을. yearA와 yearB라는 두 개의 간단한 data.frames를 제공하고 예제 Mycalculus.R 파일에서 간단한 함수 하나만 수행하십시오.이렇게하면 다른 사람들이 귀하의 질문의 본질을 더 쉽게 이해할 수 있습니다. – JasonAizkalns