Re: [R] data frame row statistics (mean)?

2010-06-28 Thread Joshua Wiley
Hello Doug,

I just wanted to add that a faster way to initialize a vector is:

avg <- vector("numeric", nrow(d))

Also you might like nrow(d) over length(d[ , 1]) if the number of rows
is what you are after.  Its sister function is ncol() .

Best regards,

Josh


On Mon, Jun 28, 2010 at 11:37 AM, Douglas M. Hultstrand
 wrote:
> Hello,
>
> I am trying to calculate the mean value of each row in a data frame (d), I
> am having troubles and getting errors using the code I have written.  Below
> is a brief example of the code, any thought or suggestions would be great.
>
> Thank you for your time,
> Doug
>
>
> # Example Code:
> d <- data.frame(st1=c(1,2,3,4), st2=c(2,5,6,7), st3=c(5,5,NA,7),
> st4=c(6,5,7,8))
> avg <- rep(NA,length(d[,1]))
>
> for (i in 1:length(d[,1])) {
>       avg[i] = mean(d[i,1:4], na.rm=TRUE)
> }
>
> # Final Output wanted.
>  st1 st2 st3 st4  avg
> 1   1   2   5   6 3.50
> 2   2   5   5   5 4.25
> 3   3   6  NA   7 5.33
> 4   4   7   7   8 6.50
>
> --
> -
> Douglas M. Hultstrand, MS
> Senior Hydrometeorologist
> Metstat, Inc. Windsor, Colorado
> voice: 720.771.5840
> email: dmhul...@metstat.com
> web: http://www.metstat.com
>
> __
> R-help@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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

__
R-help@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.


Re: [R] data frame row statistics (mean)?

2010-06-28 Thread Erik Iverson



Douglas M. Hultstrand wrote:

Hello,

I am trying to calculate the mean value of each row in a data frame (d), 
I am having troubles and getting errors using the code I have written.  
Below is a brief example of the code, any thought or suggestions would 
be great.


Thank you for your time,
Doug


# Example Code:
d <- data.frame(st1=c(1,2,3,4), st2=c(2,5,6,7), st3=c(5,5,NA,7), 
st4=c(6,5,7,8))

avg <- rep(NA,length(d[,1]))

for (i in 1:length(d[,1])) {
   avg[i] = mean(d[i,1:4], na.rm=TRUE)
}

# Final Output wanted.
 st1 st2 st3 st4  avg
1   1   2   5   6 3.50
2   2   5   5   5 4.25
3   3   6  NA   7 5.33
4   4   7   7   8 6.50



d$avg <- rowMeans(d, na.rm = TRUE)

__
R-help@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.


Re: [R] data frame row statistics (mean)?

2010-06-28 Thread Phil Spector

Doug -
   Try


d$avg = apply(d,1,mean,na.rm=TRUE)
d

  st1 st2 st3 st4  avg
1   1   2   5   6 3.50
2   2   5   5   5 4.25
3   3   6  NA   7 5.33
4   4   7   7   8 6.50

(If you must use a loop, calculate

mean(as.numeric(d[i,1:4]))

Take a look at  mean(d[1,1:4]) to see why your 
program doesn't work properly.)



- Phil Spector
 Statistical Computing Facility
 Department of Statistics
 UC Berkeley
 spec...@stat.berkeley.edu


On Mon, 28 Jun 2010, Douglas M. Hultstrand wrote:


Hello,

I am trying to calculate the mean value of each row in a data frame (d), I am 
having troubles and getting errors using the code I have written.  Below is a 
brief example of the code, any thought or suggestions would be great.


Thank you for your time,
Doug


# Example Code:
d <- data.frame(st1=c(1,2,3,4), st2=c(2,5,6,7), st3=c(5,5,NA,7), 
st4=c(6,5,7,8))

avg <- rep(NA,length(d[,1]))

for (i in 1:length(d[,1])) {
  avg[i] = mean(d[i,1:4], na.rm=TRUE)
}

# Final Output wanted.
st1 st2 st3 st4  avg
1   1   2   5   6 3.50
2   2   5   5   5 4.25
3   3   6  NA   7 5.33
4   4   7   7   8 6.50

--
-
Douglas M. Hultstrand, MS
Senior Hydrometeorologist
Metstat, Inc. Windsor, Colorado
voice: 720.771.5840
email: dmhul...@metstat.com
web: http://www.metstat.com

__
R-help@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.



__
R-help@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.


[R] data frame row statistics (mean)?

2010-06-28 Thread Douglas M. Hultstrand

Hello,

I am trying to calculate the mean value of each row in a data frame (d), 
I am having troubles and getting errors using the code I have written.  
Below is a brief example of the code, any thought or suggestions would 
be great.


Thank you for your time,
Doug


# Example Code:
d <- data.frame(st1=c(1,2,3,4), st2=c(2,5,6,7), st3=c(5,5,NA,7), 
st4=c(6,5,7,8))

avg <- rep(NA,length(d[,1]))

for (i in 1:length(d[,1])) {
   avg[i] = mean(d[i,1:4], na.rm=TRUE)
}

# Final Output wanted.
 st1 st2 st3 st4  avg
1   1   2   5   6 3.50
2   2   5   5   5 4.25
3   3   6  NA   7 5.33
4   4   7   7   8 6.50

--
-
Douglas M. Hultstrand, MS
Senior Hydrometeorologist
Metstat, Inc. Windsor, Colorado
voice: 720.771.5840
email: dmhul...@metstat.com
web: http://www.metstat.com

__
R-help@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.