简体   繁体   English

Apply()函数应用于数据框

[英]Apply() function to a data frame

I have a data frame object which has 24 columns and each one has a different length. 我有一个数据框对象,该对象有24列,每一列都有不同的长度。 I would like to multiply every column by a vector of 24 values. 我想将每列乘以24个值的向量。 I am thinking about using the apply function since I do not have any matrix. 我正在考虑使用apply函数,因为我没有任何矩阵。 my guess is like: 我的猜测是:

trans_temp:
                    Ta.f Ta.f Ta.f Ta.f
1995-10-13 04:00:00 13.6 13.6 13.6 13.6
1995-10-13 05:00:00 13.6 13.6 13.6 13.6
1995-10-13 06:00:00 13.6 13.6 13.6 13.6
1995-10-13 07:00:00 13.5 13.5 13.5 13.5
1995-10-13 08:00:00 13.5 13.5 13.5 13.5

and my vector is 而我的载体是

    x <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)

So I want the first column multiplied by 1, the second by 2, the third by 3 and so on. 因此,我希望第一列乘以1,第二列乘以2,第三列乘以3,依此类推。 I can not multiply directlly because it is a data.frame object. 我不能直接相乘,因为它是一个data.frame对象。

apply(trans_temp,x,MARGIN=2,fun) 套用(trans_temp,x,MARGIN = 2,fun)

Any help? 有什么帮助吗?

You can create a matrix directly and just multiply the data with it: 您可以直接创建一个矩阵,然后将数据与其相乘:

as.matrix(trans_temp) * col(trans_temp)

Benchmarking with eddi's 用eddi进行基准测试

m <- as.data.frame(matrix(runif(1e7), ncol=1000))
x <- seq_len(1000)
system.time(tt1 <- as.matrix(m) * col(m)) # 0.335 seconds
system.time(tt2 <- t(x*t(m))) # 0.505 seconds
identical(tt1, tt2) # TRUE

You are on the right track, but I don't understand how your columns have different lengths, unless you mean some contain, eg NA in them. 您走的路是正确的,但是我不理解您的列的长度如何不同,除非您表示其中包含某些内容,例如NA。 Use MARGIN = 1 to apply across rows. 使用MARGIN = 1应用。

x <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
t( apply(trans_temp , MARGIN = 1 , function(y) x * y ) )

You could even shorten the call like so: 您甚至可以像这样缩短通话时间:

 t( apply(trans_temp , 1 , `*` , x ) )

Here's another approach without using apply , that relies on R recycling behavior: 这是不使用apply的另一种方法,它依赖于R回收行为:

t(x*t(trans_temp))

This will probably be much faster than the other two approaches. 这可能比其他两种方法快得多。

^^^ Not anymore after Arun's edits :) What this has going for it now is that you can have an arbitrary x (and if you want an arbitrary operation in addition to arbitrary x , then you'd go with Simon's answer). ^^^在Arun进行编辑之后不再是:)现在要做的是可以拥有一个任意的x (并且,如果您想要除任意x之外还想要一个任意运算 ,则可以使用Simon的答案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM