I would like to calculate the mean of tree leader increment growth over 5 years (I1 through I5) where each tree is a row and each row has 5 columns. So far I have achieved this using rowMeans when all columns are numeric type and used in the calculation: Data1 <- data.frame(cbind(I1 = 3, I2 = c(0,3:1, 2:5,NA), I3 =c(1:4,NA,5:2),I4=2,I5=3)) Data1 Data1$mean_5 <- rowMeans(Data1, na.rm =T) Data1 My real dataset has many columns including several Factor type. Is it possible to specify a range of columns within a data frame using rowMeans dims= (say where there is a one Factor column called Species leading I1 to I5, and one Factor column called Moisture following, so 7 columns total) , or do I either need to extract those columns to a new data frame, calculate means, and reattach to the original data frame, or use a different function such as apply? Unfortunately I am fairly new to R and have a difficult time with some terminology and concepts in R Help. -- View this message in context: http://www.nabble.com/rowMean%2C-specify-subset-of-columns-within-Dataframe--tf4871420.html#a13939204 Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Charles C. Berry
2007-Nov-25 21:01 UTC
[R] rowMean, specify subset of columns within Dataframe?
On Sun, 25 Nov 2007, aaront wrote:> > I would like to calculate the mean of tree leader increment growth over 5 > years (I1 through I5) where each tree is a row and each row has 5 columns. > So far I have achieved this using rowMeans when all columns are numeric type > and used in the calculation: > > Data1 <- data.frame(cbind(I1 = 3, I2 = c(0,3:1, 2:5,NA), I3 > =c(1:4,NA,5:2),I4=2,I5=3)) > Data1Try !sapply( Data1, is.factor ) then Data_2 <- transform( Data1, I5.factor=factor(I5) ) !sapply( Data_2, is.factor ) then rowMeans( Data_2[ , !sapply( Data_2, is.factor ) ], na.rm = TRUE ) See ?Subscript ?sapply HTH, Chuck> Data1$mean_5 <- rowMeans(Data1, na.rm =T) > Data1 > > My real dataset has many columns including several Factor type. Is it > possible to specify a range of columns within a data frame using rowMeans > dims= (say where there is a one > Factor column called Species leading I1 to I5, and one Factor column called > Moisture following, so 7 columns total) , or do I either need to extract > those columns to a new data frame, calculate means, and reattach to the > original data frame, or use a different function such as apply? > > Unfortunately I am fairly new to R and have a difficult time with some > terminology and concepts in R Help. > > -- > View this message in context: http://www.nabble.com/rowMean%2C-specify-subset-of-columns-within-Dataframe--tf4871420.html#a13939204 > Sent from the R help mailing list archive at Nabble.com. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Bill.Venables at csiro.au
2007-Nov-25 23:16 UTC
[R] rowMean, specify subset of columns within Dataframe?
Suppose you want to select column 1, 3 and 4 only.
### you don't need the cbind(...) here
Data1 <- data.frame(I1 = 3, I2 = c(0, 3:1, 2:5, NA),
I3 = c(1:4, NA, 5:2), I4 = 2, I5 = 3)
Data1$mean_134 <- rowMeans(Data1[, c(1, 3:4)], na.rm = TRUE)
Data1
Bill Venables.
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of aaront
Sent: Monday, 26 November 2007 6:02 AM
To: r-help at r-project.org
Subject: [R] rowMean, specify subset of columns within Dataframe?
I would like to calculate the mean of tree leader increment growth over
5
years (I1 through I5) where each tree is a row and each row has 5
columns.
So far I have achieved this using rowMeans when all columns are numeric
type
and used in the calculation:
Data1 <- data.frame(cbind(I1 = 3, I2 = c(0,3:1, 2:5,NA), I3
=c(1:4,NA,5:2),I4=2,I5=3))
Data1
Data1$mean_5 <- rowMeans(Data1, na.rm =T)
Data1
My real dataset has many columns including several Factor type. Is it
possible to specify a range of columns within a data frame using
rowMeans
dims= (say where there is a one
Factor column called Species leading I1 to I5, and one Factor column
called
Moisture following, so 7 columns total) , or do I either need to extract
those columns to a new data frame, calculate means, and reattach to the
original data frame, or use a different function such as apply?
Unfortunately I am fairly new to R and have a difficult time with some
terminology and concepts in R Help.
--
View this message in context:
http://www.nabble.com/rowMean%2C-specify-subset-of-columns-within-Datafr
ame--tf4871420.html#a13939204
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
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.