从函数返回数据框并将其存储在工作空间中 [英] Return a dataframe from a function and store it in the workspace
问题描述
这是我与R的第一周工作,有一件事情我似乎无法管理。
df< ; - data.frame(a = c(1:10),
b = c(a,a,b,c,c,b,a c,c,b))
testF = function(select){
dum = subset(df,b == select)
}
lapply(unique(df $ b),testF)
在屏幕上打印数据集。但是我想将结果存储在我的工作空间中的单独数据帧。在这个例子中,这将给出三个数据帧; a,b和c。
感谢帮助。
Roland针对具体问题提供了正确的解决方案:不需要 split()
。只要确保: split()
返回列表。要在您的工作空间中获取单独的数据框,请执行以下操作:
list2env(split(df,df $ b),。GlobalEnv)
或使用assign:
<$ p $ (i,tmp [[i]])
子集上的一个字 这就是说,为什么你的函数是错误的呢?首先,在 / strong> 这是一个方便的功能,旨在交互使用。对于 转换为:从来没有在你的生活中再次使用 从函数返回值的单词 旁边,一个函数总是返回一个结果: 在你的情况下,最后一行包含一个作业。现在一个作业也返回一个值,但是你看不到它。 c 这绝对不必要。这就是为什么您的函数在 你的函数中的赋值是没有意义的:你明确地返回 更正您的功能 所以,例如,只需使用 或简单地: This is my first week working with R and there is one thing about function I cannot seems to manage. This function now just prints the the data sets on screen. But I would like to store the results as separate data frames in my workspace. In this example this would give three data frames; a, b and c. Thank for the help. Roland has the correct solution for the specific problem: more than a Or, using assign:
A word on subset This said, some more detail on why your function is plain wrong. First of all, in Warning This is a convenience function intended for use interactively. For
programming it is better to use the standard subsetting functions like
[, and in particular the non-standard evaluation of argument subset
can have unanticipated consequences. Translates to: Never ever in your life use A word on returning values from a function Next to that, a function always returns a result: In your case, the last line contains an assignment. Now an assignment also returns a value, but you don't see it. It's returned This is absolutely unnecessary. It's the reason why your function works when used in The assignment in your function doesn't make sense: either you return Correcting your function So, given this is just an example and the real problem wouldn't be solved by simply using or simply:
这篇关于从函数返回数据框并将其存储在工作空间中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! tmp< - split(df,df $ b) code>
?子集
中,您阅读:
编程,最好使用标准子集功能,如
[,特别是参数子集
的非标准评估可能会产生意想不到的后果。
subset()
。
return()
语句被使用,它返回任何作为参数给出的值到 return()
。
。您可以将其包装在括号中,例如:
> x< - 10
> (x < - 20)
[1] 20
lapply()
(lapply catchches invisible output)中使用时的原因,但是在命令行中使用时不会给出任何(可见)输出。您可以通过以下方式捕获它:
> testF(b)
> x< - testF(b)
> x
ab
3 3 b
6 6 b
10 10 b
dum
,或者你只是把这个赋值全部放在
split()
,您的功能将是:
testF< - function(select){
dum< - df [df $ b = select,]
return(dum)
}
testF< - function(select){
df [df $ b = select,]
}
df <- data.frame(a = c(1:10),
b = c("a", "a", "b", "c", "c", "b", "a", "c", "c", "b"))
testF = function(select) {
dum = subset(df, b == select)
}
lapply(unique(df$b), testF)
split()
is not needed. Just to make sure: split()
returns a list. To get separate data frames in you workspace, you do:list2env(split(df,df$b),.GlobalEnv)
tmp <- split(df,df$b)
for(i in names(tmp)) assign(i,tmp[[i]])
?subset
you read:
subset()
within a function again.
return()
statement is used, it returns whatever is given as an argument to return()
.invisibly
. You can see it by wrapping it in parentheses, for example:> x <- 10
> (x <- 20)
[1] 20
lapply()
(lapply catches invisible output), but won't give you any (visible) output when used at the command line. You can capture it though :> testF("b")
> x <- testF("b")
> x
a b
3 3 b
6 6 b
10 10 b
dum
explicitly, or you just drop the assignment alltogether
split()
your function would be :testF <- function(select) {
dum <- df[df$b=select,]
return(dum)
}
testF <- function(select){
df[df$b=select,]
}