为R中Vectorize()的输出创建列名? [英] Creating column names for the output of `Vectorize()` in R?
本文介绍了为R中Vectorize()的输出创建列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有创建两个列名的方法 x1
和 x2
Vectorize R
函数的输出(见下图) :
正常< - Vectorize(function(n){
x< - rnorm(n )
mean < - mean(x)
sd < - sd(x)
return(c(mean = mean,sd = sd))
})
#使用示例:
正常(n = c(1e2,1e3))
解决方案
由于默认情况下 USE.NAMES
为 TRUE
,所以名称会传递到输出。
正常(n = c(X1 = 1e2,X2 = 1e3))
或者你可以在你的函数中设置名字
pre $ normal< - function x){
if(is.null(names(x)))names(x)< - paste0('x',1:length(x))
fun< - Vectorize (x)
sd < - sd(x)
return(c)(b) (平均值=平均值,sd = sd))
}
)
fun(x)
}
#使用示例:
正常(c(1e2,1e3))
I was wondering if there is a way to create the two column names x1
and x2
for the output (see picture below) of the following Vectorize
R
function:
normal <- Vectorize(function(n){
x <- rnorm(n)
mean <- mean(x)
sd <- sd(x)
return(c(mean = mean, sd = sd))
})
# Example of use:
normal(n = c(1e2, 1e3))
解决方案
Just name your input vector. Since USE.NAMES
is TRUE
by default, the names carry through to the output.
normal(n = c(X1 = 1e2, X2 = 1e3))
Or you can set the names inside your function
normal <- function(x){
if(is.null(names(x))) names(x) <- paste0('x', 1:length(x))
fun <- Vectorize(
function(n){
x <- rnorm(n)
mean <- mean(x)
sd <- sd(x)
return(c(mean = mean, sd = sd))
}
)
fun(x)
}
# Example of use:
normal(c(1e2, 1e3))
这篇关于为R中Vectorize()的输出创建列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文