Hi,
How can I achieve this in R. Dataset is as follows:
>df
x
1 2
2 4
3 1
4 3
5 3
6 2
structure(list(x = c(2, 4, 1, 3, 3, 2)), .Names = "x", row.names =
c("1",
"2", "3", "4", "5", "6"),
class = "data.frame")
I want to recreate a new data frame whose rows are sum of (1&2, 3&4,
5&6) of original df. For example
>newdf
x
1 6
2 4
3 5
Thanx in advance for the help.
Sachin
---------------------------------
[[alternative HTML version deleted]]
Try this using DF since df is the name of an R function:
# test data from post
DF <- structure(list(x = c(2, 4, 1, 3, 3, 2)), .Names = "x",
row.names = c("1",
"2", "3", "4", "5", "6"),
class = "data.frame")
rowsum(DF, gl(nrow(DF),2,nrow(DF)))
On 7/14/06, Sachin J <sachinj.2006 at yahoo.com>
wrote:> Hi,
>
> How can I achieve this in R. Dataset is as follows:
>
> >df
> x
> 1 2
> 2 4
> 3 1
> 4 3
> 5 3
> 6 2
>
> structure(list(x = c(2, 4, 1, 3, 3, 2)), .Names = "x", row.names
= c("1",
> "2", "3", "4", "5", "6"),
class = "data.frame")
>
> I want to recreate a new data frame whose rows are sum of (1&2,
3&4, 5&6) of original df. For example
>
> >newdf
> x
> 1 6
> 2 4
> 3 5
>
> Thanx in advance for the help.
>
> Sachin
>
>
>
> ---------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>
> x <- " x+ 1 2 + 2 4 + 3 1 + 4 3 + 5 3 + 6 2 + "> x <- read.table(textConnection(x), header=TRUE) > data.frame(sum=tapply(x$x, rep(1:(nrow(x) / 2), each=2), sum))sum 1 6 2 4 3 5 On 7/14/06, Sachin J <sachinj.2006@yahoo.com> wrote:> > Hi, > > How can I achieve this in R. Dataset is as follows: > > >df > x > 1 2 > 2 4 > 3 1 > 4 3 > 5 3 > 6 2 > > structure(list(x = c(2, 4, 1, 3, 3, 2)), .Names = "x", row.names = c("1", > "2", "3", "4", "5", "6"), class = "data.frame") > > I want to recreate a new data frame whose rows are sum of (1&2, 3&4, 5&6) > of original df. For example > > >newdf > x > 1 6 > 2 4 > 3 5 > > Thanx in advance for the help. > > Sachin > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Jim Holtman Cincinnati, OH +1 513 646 9390 (Cell) +1 513 247 0281 (Home) What is the problem you are trying to solve? [[alternative HTML version deleted]]