从函数返回数据框 [英] Return a data frame from function
问题描述
我有一个函数中的以下代码
I have the following code inside a function
Myfunc<- function(directory, MyFiles, id = 1:332) {
# uncomment the 3 lines below for testing
#directory<-"local"
#id=c(2, 4)
#MyFiles<-c(f2.csv,f4.csv)
idd<-id
df2 <- data.frame()
for(i in 1:length(idd)) {
EmptyVector <- read.csv(MyFiles[i])
comp_cases[i]<-sum(complete.cases(EmptyVector))
print(comp_cases[[i]])
id=idd[i]
ret2=comp_cases[[i]]
df2<-rbind(df2,data.frame(id,ret2))
}
print(df2)
return(df2)
}
我尝试通过选择函数内的代码并注释掉返回来在R中运行它。我得到一个很好的数据框架,如打印语句:
This works when I try to run it in R by selecting the code inside the function and commenting out the return. I get a nice data frame like from the print statement:
> df2
id ret2
1 2 994
2 4 7112
但是,当我尝试从函数返回数据框 df2
时,它只返回第一行,忽略所有其他值。我的问题是它的功能在我尝试过的各种值(打开多个文件与各种组合)的功能,而不是当我尝试返回数据框。有人可以帮忙吗非常感谢。
However, when I try to return the dataframe df2
from the function it only returns the 1st row, ignoring all other values. My problem is that it works within the function for various values I have tried (opening multiple files with various combinations) and not when I try to return the data frame. Can someone help please. Thanks a lot in advance.
推荐答案
如果我理解正确,您正在尝试创建一个数据框,每个 id
。假设您的文件是与您指定的ID号相同的名称(例如 f2.csv
),可以按如下方式简化您的功能:
If I understand you correctly, you are trying to create a dataframe with the number of complete cases for each id
. Supposing your files are names with the id-numbers like you specified (e.g. f2.csv
), you can simplify your function as follows:
myfunc <- function(directory, id = 1:332) {
y <- vector()
for(i in 1:length(id)){
x <- id
y <- c(y, sum(complete.cases(
read.csv(as.character(paste0(directory,"/","f",id[i],".csv"))))))
}
df <- data.frame(x, y)
colnames(df) <- c("id","ret2")
return(df)
}
您可以这样调用此功能:
You can call this function like this:
myfunc("name-of-your-directory",25:87)
上述代码的说明。您必须将问题分解为以下步骤:
An explanation of the above code. You have to break down your problem into steps:
- 您需要一个id的向量,这是通过
x对于每个
id
,您需要完整案例的数量,< - id - 为了得到这个,你必须先阅读文件。这是通过
read.csv(as.character(paste0(directory,/,f,id [i],csv)))
完成的。要获取该文件的完整案例数,您必须将read.csv
代码包裹在sum
和complete.cases
。 - 现在,您要将该数字添加到向量中。因此,您需要一个空向量(
y< - vector()
),您可以向其中添加步骤2中的完整案例数。这是通过将代码从步骤2y < - c(y,代码步骤2)
。这样,您可以将每个id
的完整案例数添加到向量y
。 - 最后一步是将这两个向量组合到具有
df< - data.frame(x,y)
的数据框中,并分配一些有意义的colnames
。
- You need a vector of the id's, that's done by
x <- id
- For each
id
you want the number of complete cases. In order to get that, you have to read the file first. That's done byread.csv(as.character(paste0(directory,"/","f",id[i],".csv")))
. To get the number of complete cases for that file, you have to wrap theread.csv
code insidesum
andcomplete.cases
. - Now you want to add that number to a vector. Therefore you need an empty vector (
y <- vector()
) to which you can add the number of complete cases from step 2. That's done by wrapping the code from step 2 insidey <- c(y, "code step 2")
. With this you add the number of complete cases for eachid
to the vectory
. - The final step is to combine these two vectors into a dataframe with
df <- data.frame(x, y)
and assign some meaningfullcolnames
.
通过包括步骤1,2和3( y< - vector()
part)在for循环中,可以遍历指定的id的列表。使用 y< - vector()
创建空向量必须在for循环之前完成,以便for循环可以将值添加到 y
。
By including the steps 1, 2 and 3 (except the y <- vector()
part) in a for-loop, you can iterate over the list of specified id's. Creating the empty vector with y <- vector()
has to be done before the for-loop, so that the for-loop can add values to y
.
这篇关于从函数返回数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!