向量化我的想法:R 中的向量运算 [英] Vectorize my thinking: Vector Operations in R

查看:41
本文介绍了向量化我的想法:R 中的向量运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以早些时候我回答了我自己关于在 R 中思考向量的问题.但现在我遇到了另一个我无法向量化"的问题.我知道向量更快,循环更慢,但我不知道如何在向量方法中做到这一点:

So earlier I answered my own question on thinking in vectors in R. But now I have another problem which I can't 'vectorize.' I know vectors are faster and loops slower, but I can't figure out how to do this in a vector method:

我有一个数据框(出于情感原因,我喜欢将其称为 my.data),我想对其进行完整的边际分析.我需要一次删除某些元素并对数据框赋值",然后我需要通过仅删除下一个元素来再次进行迭代.然后再做一次……再一次……这个想法是对我的数据子集进行完整的边际分析.无论如何,我无法想象如何以矢量有效的方式做到这一点.

I have a data frame (which for sentimental reasons I like to call my.data) which I want to do a full marginal analysis on. I need to remove certain elements one at a time and 'value' the data frame then I need to do the iterating again by removing only the next element. Then do again... and again... The idea is to do a full marginal analysis on a subset of my data. Anyhow, I can't conceive of how to do this in a vector efficient way.

我已经缩短了代码的循环部分,它看起来像这样:

I've shortened the looping part of the code down and it looks something like this:

for (j in my.data$item[my.data$fixed==0]) { # <-- selects the items I want to loop 
                                            #     through
    my.data.it <- my.data[my.data$item!= j,] # <-- this kicks item j out of the list
    sum.data <-aggregate(my.data.it, by=list(year), FUN=sum, na.rm=TRUE) #<-- do an
                                                                         # aggregation

    do(a.little.dance) && make(a.little.love) -> get.down(tonight) # <-- a little
                                                                   #  song and dance

    delta <- (get.love)                                         # <-- get some love
    delta.list<-append(delta.list, delta, after=length(delta.list)) #<-- put my love
                                                                    #    in a vector 
}

所以很明显我在中间砍了一堆东西,只是为了让它不那么笨拙.目标是使用更有效的向量来删除 j 循环.有什么想法吗?

So obviously I hacked out a bunch of stuff in the middle, just to make it less clumsy. The goal would be to remove the j loop using something more vector efficient. Any ideas?

推荐答案

这里是另一种非常 R 类型的生成总和的方法.生成一个与输入向量一样长的向量,只包含 n 个元素的重复总和.然后,从 sums 向量中减去原始向量.结果:一个向量 (isums),其中每个条目都是原始向量减去第 i 个元素.

Here's what seems like another very R-type way to generate the sums. Generate a vector that is as long as your input vector, containing nothing but the repeated sum of n elements. Then, subtract your original vector from the sums vector. The result: a vector (isums) where each entry is your original vector less the ith element.

> (my.data$item[my.data$fixed==0])
[1] 1 1 3 5 7
> sums <- rep(sum(my.data$item[my.data$fixed==0]),length(my.data$item[my.data$fixed==0]))
> sums
[1] 17 17 17 17 17
> isums <- sums - (my.data$item[my.data$fixed==0])
> isums
[1] 16 16 14 12 10

这篇关于向量化我的想法:R 中的向量运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆