Re: [R] Create a new dataframe from an existing dataframe

From: jim holtman <jholtman_at_gmail.com>
Date: Mon, 7 Jan 2008 19:39:28 -0500

Does this do what you want?

> x <- read.table(textConnection("A B C D

+ A1  B1 C1 D1
+ A1  B2 C1 D1
+ A1  B1 C1 D1
+ A1  B2 C2 D2
+ A2  B1 C1 D1
+ A1  B2 C2 D2
+ A1  B1 C2 D2
+ A2  B2 C1 D1
+ A1  B1 C2 D2
+ A2  B2  C1 D1"), header=TRUE)

> counts <- ave(seq(nrow(x)), x$B, x$C, x$D, FUN=length) # get counts
> x.new <- x[, -1] # delete "A"
> x.new$FreqD <- counts # add new column
> # print out unique entries
> unique(x.new)

   B C D FreqD

1 B1 C1 D1     3
2 B2 C1 D1     3
4 B2 C2 D2     2
7 B1 C2 D2     2

>

On Jan 7, 2008 2:38 PM, Gang Chen <gangchen_at_mail.nih.gov> wrote:
> Yes, I misstated it when I said that I would keep B and C. I want to
> collapse column A, but count the frequency of D as a new column in
> the new dataframe DF2 while collapsing A. The rows of columns B, C,
> and D of course would be reduced because of A collapsing.
>
> For example, if dataframe DF is
>
> A B C D
> A1 B1 C1 D1
> A1 B2 C1 D1
> A1 B1 C1 D1
> A1 B2 C2 D2
> A2 B1 C1 D1
> A1 B2 C2 D2
> A1 B1 C2 D2
> A2 B2 C1 D1
> A1 B1 C2 D2
> A2 B2 C1 D1
> ......
>
> I would like to have a new dataframe DF2
>
> B C D FreqA
> B1 C1 D1 6
> B1 C1 D2 31
> B1 C2 D1 8
> B1 C2 D2 14
> B2 C1 D1 12
> B2 C1 D2 43
> B2 C2 D1 23
> B2 C2 D2 43
>
> Thanks,
> Gang
>
>
>
>
> On Jan 7, 2008, at 2:06 PM, Duncan Murdoch wrote:
>
> > On 1/7/2008 1:28 PM, Gang Chen wrote:
> >> I have a dataframe DF with 4 columns (variables) A, B, C, and D,
> >> and want to create a new dataframe DF2 by keeping B and C in DF
> >> but counting the frequency of D while collapsing A. I tried
> >> by(DF$D, list(DF$B, DF$C), FUN=summary)
> >> but this is not exactly what I want. What is a good way to do it?
> >
> > I think you can't do that. If you want to keep B and C, then you
> > can't reduce the number of rows, but "collapsing A" sounds like you
> > want fewer rows.
> >
> > Perhaps if you posted a simple before and after example?
> >
> > Duncan Murdoch
>
> ______________________________________________
> 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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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.
Received on Tue 08 Jan 2008 - 00:45:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 Jan 2008 - 16:30:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive