類似の質問は既に聞かれましたが、私の特定の問題を解決できませんでした。私は.R
ファイル( "Mycalculus.R")は、データフレームのサブセットに適用する必要がある多くの基本的な計算を含んでいます: "year"のモダリティが数値でない(yearA、yearB、yearC)値。このファイルは、Rdaファイルに保存する必要がある新しいデータフレームを生成します。ここでは(この1つは明らかに動作しません)私はコードがfor
ループでのように見えることを期待するものである:ここではサブセットをループし、ファイルをソースにして結果をデータフレームに保存します
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のDFの正確である:
ここ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
ソースファイル "Mycalculus.R"は、 "データ"と呼ばれるデータフレームの列に多数の基本計算を適用し、df1に基づいて2つの新しいデータフレーム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")
最後に、私はこのような2つのデータフレームを得るようにするため:
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 ....
現在、私のプログラムは、任意のループを使用していないと仕事をしてますが乱雑されます。基本的にコードは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)
:あなたの手順を機能-IZE場合
再現性の一例とするだけで、あなたの質問。 yearAとyearBの2つの単純なdata.framesを用意し、Mycalculus.Rファイルの例では単純な関数を1つ実行します。これにより、他の人があなたの質問の性質を理解しやすくなります。 – JasonAizkalns