[R] data.frame-question]

Petr Pikal petr.pikal at precheza.cz
Thu Oct 27 09:18:57 CEST 2005


Hi

quite near

using aggregate it is possible to reach what you want

TAB3 <- with(TAB1, aggregate(Number, 
list("Name_singular"=Name), sum, na.rm=TRUE))

see
> str(TAB3)
`data.frame':   3 obs. of  2 variables:
 $ Name_singular: Factor w/ 3 levels "A","B","C": 1 2 3
 $ x            : num  3 5 0
>

HTH
Petr


On 27 Oct 2005 at 8:47, Brandt, T. (Tobias) wrote:

From:           	"Brandt, T. (Tobias)" <TobiasBr at taquanta.com>
To:             	"'Michael Graber'" <michael_graber at gmx.de>,
	"'r-help at stat.math.ethz.ch'" <r-help at stat.math.ethz.ch>
Date sent:      	Thu, 27 Oct 2005 08:47:45 +0200
Subject:        	Re: [R] data.frame-question]

> First a general comment on posting style, could you please be more
> specific about where the error occurs as without this it is very
> difficult to identify what the problem is.
> 
> Now concerning your problem.  When I tried the code I posted yesterday
> I thought it worked fine.  I've tried it again now and found that the
> data.frame TAB3 actually only has one column and the names "A", "B"
> etc are actually interpreted as the row names.  Since there is only
> one column the 'colnames(TAB3) <-' fails when you give it a vector
> with two components.  I think that if you display it without renaming
> the columns then it still displays the correct results though.  I also
> tested it with including NA's and it worked fine.  I'm quite a newbie
> myself so I don't know how you can't tell you how to return the result
> into a two column data.frame.
> 
> For completeness, here is the code I used to test.
> 
> > Name <- c(rep("A", 3), rep("B", 5), "C")
> > Number <- rep(1, 8)
> > Number[9] <- NA
> > TAB1 <- data.frame(Name, Number)
> > TAB1
>   Name Number
> 1    A      1
> 2    A      1
> 3    A      1
> 4    B      1
> 5    B      1
> 6    B      1
> 7    B      1
> 8    B      1
> 9    C     NA
> > TAB3 <- with(TAB1, tapply(Number, Name, sum, na.rm=TRUE))
> > TAB3
> A B C 
> 3 5 0 
> > TAB3 <- as.data.frame(TAB3)
> > TAB3
>   TAB3
> A    3
> B    5
> C    0
> > colnames(TAB3) <- c("Name_singular", "Sum")
> Error in "dimnames<-.data.frame"(`*tmp*`, value = list(c("A", "B", "C"
> : 
>         invalid 'dimnames' given for data frame
> > TAB3
>   TAB3
> A    3
> B    5
> C    0
> > str(TAB3)
> `data.frame':   3 obs. of  1 variable:
>  $ TAB3: num [, 1:3] 3 5 0
>   ..- attr(*, "dimnames")=List of 1
>   .. ..$ : chr  "A" "B" "C"
> > 
> > version
>          _              
> platform i386-pc-mingw32
> arch     i386           
> os       mingw32        
> system   i386, mingw32  
> status                  
> major    2              
> minor    2.0            
> year     2005           
> month    10             
> day      06             
> svn rev  35749          
> language R              
> 
> 
> 
> -----Original Message-----
> From: Michael Graber [mailto:michael_graber at gmx.de] 
> Sent: 27 October 2005 12:43 AM
> To: TobiasBr at Taquanta.com
> Subject: Re: Re: [R] data.frame-question]
> 
> This is what I am looking for, but I still get an error message, that
> my arguments are not of the same length. How can I avoid this error
> message? Maybe I should add, that there are also NA´s in the second
> column, but I tried to ignore them by na.rm=TRUE.
> 
> Thanks in advance,
> 
> Michael Graber
> 
> 
> 
> Michael Graber schrieb:
> 
> >
> >
> > --------------------------------------------------------------------
> > -- --
> >
> > Betreff:
> > RE: [R] data.frame-question
> > Von:
> > "Brandt, T. (Tobias)" <TobiasBr at Taquanta.com>
> > Datum:
> > Wed, 26 Oct 2005 09:20:23 +0200
> > An:
> > "'r-help at stat.math.ethz.ch'" <r-help at stat.math.ethz.ch>
> >
> > An:
> > "'r-help at stat.math.ethz.ch'" <r-help at stat.math.ethz.ch>
> > CC:
> > 'Michael Graber' <michael_graber at gmx.de>
> >
> >
> > Is
> >
> > TAB3 <- as.data.frame(with(TAB1, tapply(Number, Name, sum)))
> > colnames(TAB3) <- c("Name_singular", "Sum")
> >
> > what you are looking for?
> >
> >
> >
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch 
> > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Michael
> > Graber Sent: 25 October 2005 09:45 PM To: R-Mailingliste Subject:
> > [R] data.frame-question
> >
> > Dear R-List,
> > I am very new to R and programming itself, so my question may be
> > easy to answer for you. I tried a lot and read through the manuals,
> > but I still have the following problem: I have 2 data-frames:
> > Number<-as.numeric (Number) Name<-as.character (Name)
> > TAB1<-data.frame (Name,Number) - it looks like this:- Name Number A
> > 2 A 3 A 6 B 8 B 12 B 7 C 8 D 90 E 12 E 45 ...
> > Name_singular<-as.character (Name_singular) TAB2<-data.frame
> > (Name_singular) # it looks like this: Name_singular A B C D E -My
> > result should be a data-frame, where the first column is
> > Name_singular and the second column should be the sum of the numbers
> > where Name ==Name_singular.- For example:
> >
> > TAB3:
> > Name_singular Sum
> > A 11
> > B 27
> > ...
> > - I tried it with for-loops, but I think there must be an easier
> > way.- I would be very grateful for your help,
> >
> >
> > Michael Graber
> >
> > ______________________________________________
> > 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
> >
> > --------------------------------------------------------------------
> > -- --
> >
> > Nedbank Limited Reg No 1951/000009/06
> > Directors: WAM Clewlow (Chairman)  Prof MM Katz (Vice-chairman)  ML
> > Ndlovu (Vice-chairman)  TA Boardman (Chief Executive) CJW Ball  MWT
> > Brown  RG Cottrell  BE Davison  N Dennis (British)  MA Enus-Brey 
> > Prof B de L Figaji  RM Head (British) RJ Khoza  JB Magwaza  ME
> > Mkwanazi  JVF Roberts (British)  CML Savage GT Serobe  JH Sutcliffe
> > (British) Company Secretary: GS Nienaber     16.08.2005
> >
> > This email and any accompanying attachments may contain confidential
> > and proprietary information.  This information is private and
> > protected by law and, accordingly, if you are not the intended
> > recipient, you are requested to delete this entire communication
> > immediately and are notified that any disclosure, copying or
> > distribution of or taking any action based on this information is
> > prohibited.
> >
> > Emails cannot be guaranteed to be secure or free of errors or
> > viruses.  The sender does not accept any liability or responsibility
> > for any interception, corruption, destruction, loss, late arrival or
> > incompleteness of or tampering or interference with any of the
> > information contained in this email or for its incorrect delivery or
> > non-delivery for whatsoever reason or for its effect on any
> > electronic device of the recipient.
> >
> > If verification of this email or any attachment is required, please
> > request a hard-copy version.
> >
> > --------------------------------------------------------------------
> > -- --
> 
> 
> ********************
> Nedbank Limited Reg No 1951/000009/06
> Directors: WAM Clewlow (Chairman)  Prof MM Katz (Vice-chairman)  ML
> Ndlovu (Vice-chairman)  TA Boardman (Chief Executive) CJW Ball  MWT
> Brown  RG Cottrell  BE Davison  N Dennis (British)  MA Enus-Brey  Prof
> B de L Figaji  RM Head (British) RJ Khoza  JB Magwaza  ME Mkwanazi 
> JVF Roberts (British)  CML Savage  GT Serobe  JH Sutcliffe (British)
> Company Secretary: GS Nienaber     16.08.2005
> 
> This email and any accompanying attachments may contain confidential
> and proprietary information.  This information is private and
> protected by law and, accordingly, if you are not the intended
> recipient, you are requested to delete this entire communication
> immediately and are notified that any disclosure, copying or
> distribution of or taking any action based on this information is
> prohibited.
> 
> Emails cannot be guaranteed to be secure or free of errors or viruses.
>  The sender does not accept any liability or responsibility for any
> interception, corruption, destruction, loss, late arrival or
> incompleteness of or tampering or interference with any of the
> information contained in this email or for its incorrect delivery or
> non-delivery for whatsoever reason or for its effect on any electronic
> device of the recipient.
> 
> If verification of this email or any attachment is required, please
> request a hard-copy version. ********************
> 
>  [[alternative HTML version deleted]]
> 
> 

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list