Marine Andersson
2011-Feb-10 20:53 UTC
[R] Calculating rowMeans from different columns in each row?
Hello! I have a dataset like this: X1 X2 X3 X4 X5 X6 X7 X8 1 2 2 1 2 3 2 6 2 3 2 5 7 9 1 3 1 9 12 6 1 1 3 6 The columns X1-X6 contains ordinary numeric values. X7 contains the number of the first column that the rowMeans should be calculated from and X8 contains the last column that should be included in the rowMeans. when I try test <- (df[,df$X7:df$X8]) the rowMeans are calculated based on the values in the X7 and X8 in the first row only. Thanks in advance! /Marine
rex.dwyer at syngenta.com
2011-Feb-10 21:23 UTC
[R] Calculating rowMeans from different columns in each row?
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Marine Andersson Sent: Thursday, February 10, 2011 3:53 PM To: r-help at r-project.org Subject: [R] Calculating rowMeans from different columns in each row? Hello! I have a dataset like this: X1 X2 X3 X4 X5 X6 X7 X8 1 2 2 1 2 3 2 6 2 3 2 5 7 9 1 3 1 9 12 6 1 1 3 6 The columns X1-X6 contains ordinary numeric values. X7 contains the number of the first column that the rowMeans should be calculated from and X8 contains the last column that should be included in the rowMeans. when I try test <- (df[,df$X7:df$X8]) the rowMeans are calculated based on the values in the X7 and X8 in the first row only. Thanks in advance! /Marine ______________________________________________ [Dwyer Rex USRE] Well, if you print df$X7:df$X8, you'll see why... you can't ":" together two vectors:> c(1,2,3):c(8,10,12)[1] 1 2 3 4 5 6 7 8 Warning messages: 1: In c(1, 2, 3):c(8, 10, 12) : numerical expression has 3 elements: only the first used 2: In c(1, 2, 3):c(8, 10, 12) : numerical expression has 3 elements: only the first used>So try: apply(df,1, function(v) {n=length(v); mean(v[v[n-1]:v[n]]) }) message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited.
Phil Spector
2011-Feb-10 21:58 UTC
[R] Calculating rowMeans from different columns in each row?
Marine -
Assuming your data frame is named "df", I think
apply(df,1,function(x)mean(x[x[7]:x[8]]))
will give you what you're looking for.
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Thu, 10 Feb 2011, Marine Andersson wrote:
> Hello!
>
> I have a dataset like this:
>
> X1 X2 X3 X4 X5 X6 X7 X8
> 1 2 2 1 2 3 2 6
> 2 3 2 5 7 9 1 3
> 1 9 12 6 1 1 3 6
>
> The columns X1-X6 contains ordinary numeric values.
>
> X7 contains the number of the first column that the rowMeans should be
calculated from and
> X8 contains the last column that should be included in the rowMeans.
>
> when I try
>
> test <- (df[,df$X7:df$X8])
>
> the rowMeans are calculated based on the values in the X7 and X8 in the
first row only.
>
> Thanks in advance!
>
> /Marine
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>