问题标签 [sapply]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
r - R结合mapply和sapply
我在 R 中有一个函数,它接受 3 个参数,比如foo(x,y,z)
.
当我调用该函数时,我确实有一个元素x
列表 for 和一个列表,y
但只有一个元素 for z
。IfZ
是一个列表,我想应用于foo
每个元素,mapply(foo, x, y, z)
工作。
但是,由于z
不是列表,mapply(foo,x,y,z)
因此不起作用。
更具体地说,如果x
和 y 分别是 3 个元素的列表,则以下内容确实有效:mapply(foo, x, y, list(z, z, z))
.
有没有一种方法可以让我组合起来mapply
,而sapply
无需我先将它们组合z
成一个包含 3 个元素的列表?我z
只想被重复使用!
编辑1:有人问我一个例子:
以下作品:
以下不起作用:
r - Why parameters passed by function can not be found when sapply subset to a list of dataframe in R
I met a wired problem while sapply subset to a list of dataframe inside a function that R saying "Error in eval(expr, envir, enclos) : object 'thresh' not found". I wonder why this would happen.
r - 在字符串开头之前粘贴
所以我有一个向量
我可以使用 sapply在每个元素之后粘贴字符
现在,我如何使用类似的功能在每个元素之前粘贴字符,所以我得到
r - 使用函数和 sapply 更新数据框
我试图在数据框中设置一个等于“美国”或“外国”的列,具体取决于国家/地区。我相信这样做的正确方法是编写一个函数,然后用于sapply
实际更新数据帧。这是我第一次在 in 中尝试这样的事情R
,SQL
我会写一个UPDATE
查询。
这是我的数据框:
我试图编写的函数是这样的:
然后我会像这样应用它:
当我sapply
在数据框的头部运行时,我得到了这个:
该函数似乎正确地对 Country 进行了分类,但没有正确更新 clients$CountryType 列。我究竟做错了什么?另外-这是完成更新数据框的最佳方法吗?
r - 在 R 的 sapply 调用中使用 which 的加速函数
我有两个向量e
和g
。我想知道每个元素中较小e
的元素百分比。g
在 R 中实现这一点的一种方法是:
使用大的e
or g
,这需要很长时间才能运行。如何更改或调整此代码以使其运行更快?
注意:mf
上面的函数是基于mess
dismo 包中函数的代码。
r - R: sapply 打印结果
我有这样做的代码:
但是在 RGui 窗口中,我只能向上滚动查看一些结果。前 10 个左右没有显示在窗口中(可能是因为我的数据太大)。如何让它显示所有内容,或者如何打印前 10 个结果?
谢谢。
r - 所有列的简单 z 变换
我刚刚为所有列的矩阵中的 z 转换编写了一个非常基本的函数。它看起来像这样:
sapply(MyObject, function(x){(x-mean(x))/sd(x)})
我随机检查了矩阵中某些单元格的函数,它似乎工作正常。我仍然想确认这个功能是好的,因为我对 R 很陌生,我在互联网上找不到任何好的例子。
r - R 通配符、sapply 和 as.factor
我想将类型更改为名称与特定模式匹配的数据框中所有变量的因子。
所以在这里我试图将类型更改为所有变量的因子,这些变量的名称以dataframe中的namestub 开头df
。
但这不起作用,因为
r - R: Apply function on specific columns preserving the rest of the dataframe
I'd like to learn how to apply functions on specific columns of my dataframe without "excluding" the other columns from my df. For example i'd like to multiply some specific columns by 1000 and leave the other ones as they are.
Using the sapply function for example like this:
I get new dataframes with the first column multiplied by 1000 but without the other columns that I didn't use in the operation. So my attempt was to do it like this:
but this one didn't work.
My workaround was to give both dataframes another row with IDs and later on merge the old dataframe with the newly created to get a complete one. But I think there must be a better solution. Isn't it?
r - 让 match.call() 与 *apply & plyr 一起工作?(re:递归引用数据框)
以下函数的目的是更容易地进行自引用分配。(如此处建议:递归引用数据框)
所以,而不是
这些函数适用于矢量,但在 *ply'ing 时效果较差
我遇到了关于match.call()
函数部分的两个问题selfAssign()
(代码如下)。问题是:
- 如果它是从 *apply-type 函数调用的,我如何从函数中确定它?
- 如何将调用追溯到正确的变量环境?
我在结尾处包含了适用于该n
陈述的论点。我想知道我是否可以以某种方式利用类似于selfAssign(.)
eval
n
也许在 selfAssign 中有类似的东西sys.parent(n)
(我试过了,要么我没有做对,要么它不起作用)
任何建议将不胜感激。
功能
这些函数是 selfAssign 的包装器,将在*apply
调用中使用。
selfAssign
是执行工作的函数以及错误的来源