How to combine two rows (in a dataframe) into a third row?

View: New views
4 Messages — Rating Filter:   Alert me  

How to combine two rows (in a dataframe) into a third row?

by Mark Na :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear R-helpers,

I have two rows in my dataframe:

ID    VALUE
1A    10
1B    15

and I would like to combine these two rows into a single (new) row in my
dataframe:

ID    VALUE
1    25

...simply by specifying a new value for ID and summing the two VALUES.

I have been trying to do this with with rbind, but it's not working.

I'd appreciate any pointers.

Thanks, Mark Na

        [[alternative HTML version deleted]]

______________________________________________
R-help@... 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: How to combine two rows (in a dataframe) into a third row?

by Peter Alspach :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Tena koe Mark

?tapply

with the index argument some suitable substring of your ID column.

HTH ....

Peter Alspach

> -----Original Message-----
> From: r-help-bounces@...
> [mailto:r-help-bounces@...] On Behalf Of Mark Na
> Sent: Friday, 10 July 2009 10:28 a.m.
> To: r-help@...
> Subject: [R] How to combine two rows (in a dataframe) into a
> third row?
>
> Dear R-helpers,
>
> I have two rows in my dataframe:
>
> ID    VALUE
> 1A    10
> 1B    15
>
> and I would like to combine these two rows into a single
> (new) row in my
> dataframe:
>
> ID    VALUE
> 1    25
>
> ...simply by specifying a new value for ID and summing the two VALUES.
>
> I have been trying to do this with with rbind, but it's not working.
>
> I'd appreciate any pointers.
>
> Thanks, Mark Na
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@... 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.
>

The contents of this e-mail are confidential and may be subject to legal privilege.
 If you are not the intended recipient you must not use, disseminate, distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received this
 e-mail in error, please notify the sender and delete all material pertaining to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.

______________________________________________
R-help@... 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: How to combine two rows (in a dataframe) into a third row?

by Henrique Dallazuanna :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try this:

aggregate(x["VALUE"], list(substr(x[,"ID"], 1, 1)), sum)

On Thu, Jul 9, 2009 at 7:27 PM, Mark Na <mtb954@...> wrote:

> Dear R-helpers,
>
> I have two rows in my dataframe:
>
> ID    VALUE
> 1A    10
> 1B    15
>
> and I would like to combine these two rows into a single (new) row in my
> dataframe:
>
> ID    VALUE
> 1    25
>
> ...simply by specifying a new value for ID and summing the two VALUES.
>
> I have been trying to do this with with rbind, but it's not working.
>
> I'd appreciate any pointers.
>
> Thanks, Mark Na
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@... 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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
R-help@... 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: How to combine two rows (in a dataframe) into a third row?

by Mark Na :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Henrique & other R-helpers,
Thank you for helping me last week. I used Henrique's suggestion to develop
some code (below) to combine two rows in my dataframe into a third row, and
then delete the original two rows. It works well.

My solution is not very elegant however; if there's a function (or a better
way) to accomplish this in 1-2 lines (rather than my 6) I'd appreciate
knowing about it.

Many thanks, Mark Na


#make some data for this example

data<-data.frame(c("1A","1B"),c("10","15"))

names(data)<-c("id","value")

data$value<-as.numeric(as.character(data$value))


#combine two lines into one by summing their values in the value column

fixed<-data.frame() #create empty data frame to hold fixed rows

fixed<-rbind(fixed, aggregate(data["value"],list(substr(data[,"id"],1,1)),
sum))

#copy previous line as necessary for other fixes

names(fixed)<-c("id","value") #fix column names


#bind the fixed line to the main dataframe and delete the original lines

data<-rbind(data,fixed) #add fixed lines to data

data<-data[-which(c("1A","1B") %in% data$id),] #delete lines from data

rownames(data) <- 1:nrow(data) #renumber rows



On Thu, Jul 9, 2009 at 5:58 PM, Henrique Dallazuanna <wwwhsd@...>wrote:

> Try this:
>
> aggregate(x["VALUE"], list(substr(x[,"ID"], 1, 1)), sum)
>
> On Thu, Jul 9, 2009 at 7:27 PM, Mark Na <mtb954@...> wrote:
>
>> Dear R-helpers,
>>
>> I have two rows in my dataframe:
>>
>> ID    VALUE
>> 1A    10
>> 1B    15
>>
>> and I would like to combine these two rows into a single (new) row in my
>> dataframe:
>>
>> ID    VALUE
>> 1    25
>>
>> ...simply by specifying a new value for ID and summing the two VALUES.
>>
>> I have been trying to do this with with rbind, but it's not working.
>>
>> I'd appreciate any pointers.
>>
>> Thanks, Mark Na
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@... 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.
>>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
        [[alternative HTML version deleted]]


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