【问题标题】:Take arguments from individual columns of dataframe, output into list, then bind into dataframe从数据框的各个列中获取参数,输出到列表中,然后绑定到数据框
【发布时间】:2016-03-24 16:15:43
【问题描述】:

我已经为此工作了好几个小时!有人很好地帮助我完成了这个功能——这很有效!

rnormfunc <- function(n.arg, mean.arg, sd.arg, percentdist.arg){  ## main function; first three inputs are for rnorm values, percentdist.arg is percent from mean argument

    rnorm1 <- rnorm(n.arg, mean.arg, sd.arg) # creates a vector holding values in normal distrib. from inputs 
    j <- (percentdist.arg/100)  # changes percent distance into decimal value
    b <- mean.arg*(1+j)  # max percent distance from mean
    g <- mean.arg*(1-j) # min percent distance from mean

    total.in.range <- sum(rnorm1 >= g  & rnorm1 <= b)
    return(total.in.range) # sum of number of values within percentage range                
}

我需要使用此函数并将以下矩阵/数据框用作参数。我一直在尝试各种方法——基本上是 apply() 系列函数。具体来说,mapply() 或 by() 函数。我正在为是否应该使用矩阵或数据框而苦苦挣扎——以及出于什么原因。

samp.vect <- c(1,1.96,3.92) #three randomized percent distances from mean
r.dat <- matrix(data = NA, nrow = 101, ncol = 4)
colnames(r.dat) <- c("n.arg","mean.arg","sd.arg","percentdist.arg")
r.dat[,1] = seq(from <-1, to <- 100001,by =1000) #1 to 100001 by 1000, first column
r.dat[,2] = 0  # second column is mean, always zero
r.dat[1:50,3] = c(1)   # column 3 rows 1 to 50 has sd = 1
r.dat[51:101,3]  = c(2)  # column 3 rows 51:101 has sd = 2
r.dat[,4]  = sample(samp.vect,101, replace = TRUE) 
head(r.dat)
tail(r.dat)

产生这个:

> head(r.dat)
     n.arg mean.arg sd.arg percentdist.arg
[1,]     1        0      1            3.92
[2,]  1001        0      1            1.00
[3,]  2001        0      1            1.96
[4,]  3001        0      1            1.96
[5,]  4001        0      1            3.92
[6,]  5001        0      1            1.00

所以我被困住了——我觉得我已经挂断了要么使用矩阵,要么使用数据框来启动。我觉得某种类型的 apply(rnormfunc, r.dat$n.arg, r.dat$n.mean...) 可能是一种方式,但我不知道该怎么做。

然后操作这些数据结构之一,其中四列中的每一列都需要按行用作上述 rnormfunc() 的参数。然后我想使用 cbind() 将它附加到矩阵/数据框。

不得不说,这非常令人沮丧,但同时正确学习和学习非常有趣!另外,请随时批评我的礼仪/帖子,以便更容易获得反馈!

【问题讨论】:

    标签: r


    【解决方案1】:

    我只在前 6 个案例(行)上对此进行了测试,但这应该是可以概括的:

    apply( head(r.dat), 1, function(x) 
                rnormfunc( x['n.arg'], x['mean.arg'], x['sd.arg'], x['percentdist.arg']) )
    

    【讨论】:

    • 感谢您的反馈。看看你提出的 apply() 代码,以及我在其他地方找到的东西,它是有道理的并且应该可以工作。但是,当我运行它时,它只返回零。您调用 apply() 函数,参数是数据结构(在本例中为矩阵),MARGIN = 1 用于跨行应用,然后一个函数 (x) 调用我的函数,每个参数输入都带有暗名。
    • 当我查看该函数的实际内容时,在我看来那些 0 是您提供的算法的正确答案。为什么任何情况都应该大于和小于 0????你说它“有效”,但我怀疑它“有效”。
    • @PeterSmith 如果我在 rnormfunc() 中手动输入四个唯一的参数,它会返回一个必须小于 n.arg 的值,并且取决于 sd.arg。和 percentdist.arg。对你来说不是这样吗?
    • 我不确定 Peter 是谁,但我认为 应该将前几行数据的实际值放入你的函数中,看看你会得到什么。我认为这将是全0。我认为您的逻辑运算符应该是“|”如果您试图使数字超出范围,并且如果您希望数字在范围内,则应交换不等式的方向。并且在未来你应该说出你的目标是什么。
    • 别管彼得的事了。谢谢你的帮助。我创建了一个包含四列的矩阵“r.dat”(n.arg= 样本大小,mean.arg = 平均值,sd.arg 是标准偏差,percentdist.arg 是平均值某个百分比内的值的数量,它是从“sample.vect”中的三个值中随机采样的。我的目标是使用 apply() 系列函数在每一行中对 r.dat 进行 rnormfunc() 采样,并为向量中的每一行返回一个值。如果我手动输入rnormfunc(4001,0,1,3.92),输出是1900到2100之间的一个值。我解释得好吗?谢谢你的帮助!
    猜你喜欢
    • 2010-10-23
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2019-11-19
    • 2020-09-13
    • 2012-09-06
    相关资源
    最近更新 更多