Hi R users
Imagine the table "base":
p=c("d","d","b","b","a","a")
q=c("v1","v2","v1","v2","v1","v2")
r=c(5,2,4,8,9,7)
base=data.frame(p,q,r)
base
p q r
1 d v1 5
2 d v2 2
3 b v1 4
4 b v2 8
5 a v1 9
6 a v2 7
How programming R to get this result:
v1 v2
a 9 7
b 4 8
d 5 2
I tried the function table but the result is divided in several matrix:
table(base$p,base$q,base$r)
, , = 2
v1 v2
a 0 0
b 0 0
d 0 1
, , = 4
v1 v2
a 0 0
b 1 0
d 0 0
, , = 5
v1 v2
a 0 0
b 0 0
d 1 0
, , = 7
v1 v2
a 0 1
b 0 0
d 0 0
, , = 8
v1 v2
a 0 0
b 0 1
d 0 0
, , = 9
v1 v2
a 1 0
b 0 0
d 0 0
SIncerely yours
--
View this message in context: http://r.789695.n4.nabble.com/How-to-built-a-pivot-table-of-value-tp4651539.html
Sent from the R help mailing list archive at Nabble.com.
How to built a pivot table of value
7 messages · CE.KA, Jorge I Velez, Berend Hasselman +2 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121202/5e914517/attachment.pl>
On 01-12-2012, at 13:37, CE.KA wrote:
Hi R users
Imagine the table "base":
p=c("d","d","b","b","a","a")
q=c("v1","v2","v1","v2","v1","v2")
r=c(5,2,4,8,9,7)
base=data.frame(p,q,r)
base
p q r
1 d v1 5
2 d v2 2
3 b v1 4
4 b v2 8
5 a v1 9
6 a v2 7
How programming R to get this result:
v1 v2
a 9 7
b 4 8
d 5 2
xtabs(r~p+q,data=base) Output --------- q p v1 v2 a 9 7 b 4 8 d 5 2 Berend
One way, using the plyr package would be: library(plyr) dcast(base, p ~ q) John Kane Kingston ON Canada
-----Original Message-----
From: ce.kaya75 at yahoo.fr
Sent: Sat, 1 Dec 2012 04:37:02 -0800 (PST)
To: r-help at r-project.org
Subject: [R] How to built a pivot table of value
Hi R users
Imagine the table "base":
p=c("d","d","b","b","a","a")
q=c("v1","v2","v1","v2","v1","v2")
r=c(5,2,4,8,9,7)
base=data.frame(p,q,r)
base
p q r
1 d v1 5
2 d v2 2
3 b v1 4
4 b v2 8
5 a v1 9
6 a v2 7
How programming R to get this result:
v1 v2
a 9 7
b 4 8
d 5 2
I tried the function table but the result is divided in several matrix:
table(base$p,base$q,base$r)
, , = 2
v1 v2
a 0 0
b 0 0
d 0 1
, , = 4
v1 v2
a 0 0
b 1 0
d 0 0
, , = 5
v1 v2
a 0 0
b 0 0
d 1 0
, , = 7
v1 v2
a 0 1
b 0 0
d 0 0
, , = 8
v1 v2
a 0 0
b 0 1
d 0 0
, , = 9
v1 v2
a 1 0
b 0 0
d 0 0
SIncerely yours
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-built-a-pivot-table-of-value-tp4651539.html
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.
____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails
On 01-12-2012, at 14:56, John Kane wrote:
One way, using the plyr package would be: library(plyr) dcast(base, p ~ q)
Shouldn't that be library(reshape2) ? Berend
John Kane Kingston ON Canada
-----Original Message-----
From: ce.kaya75 at yahoo.fr
Sent: Sat, 1 Dec 2012 04:37:02 -0800 (PST)
To: r-help at r-project.org
Subject: [R] How to built a pivot table of value
Hi R users
Imagine the table "base":
p=c("d","d","b","b","a","a")
q=c("v1","v2","v1","v2","v1","v2")
r=c(5,2,4,8,9,7)
base=data.frame(p,q,r)
base
p q r
1 d v1 5
2 d v2 2
3 b v1 4
4 b v2 8
5 a v1 9
6 a v2 7
How programming R to get this result:
v1 v2
a 9 7
b 4 8
d 5 2
I tried the function table but the result is divided in several matrix:
table(base$p,base$q,base$r)
, , = 2
v1 v2
a 0 0
b 0 0
d 0 1
, , = 4
v1 v2
a 0 0
b 1 0
d 0 0
, , = 5
v1 v2
a 0 0
b 0 0
d 1 0
, , = 7
v1 v2
a 0 1
b 0 0
d 0 0
, , = 8
v1 v2
a 0 0
b 0 1
d 0 0
, , = 9
v1 v2
a 1 0
b 0 0
d 0 0
SIncerely yours
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-built-a-pivot-table-of-value-tp4651539.html
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.
____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails ______________________________________________ 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.
Blast, you're right. I tend to load both at the same time and confused the two. It should be reshape2 John Kane Kingston ON Canada
-----Original Message----- From: bhh at xs4all.nl Sent: Sat, 1 Dec 2012 15:20:13 +0100 To: jrkrideau at inbox.com Subject: Re: [R] How to built a pivot table of value On 01-12-2012, at 14:56, John Kane wrote:
One way, using the plyr package would be: library(plyr) dcast(base, p ~ q)
Shouldn't that be library(reshape2) ? Berend
John Kane Kingston ON Canada
-----Original Message-----
From: ce.kaya75 at yahoo.fr
Sent: Sat, 1 Dec 2012 04:37:02 -0800 (PST)
To: r-help at r-project.org
Subject: [R] How to built a pivot table of value
Hi R users
Imagine the table "base":
p=c("d","d","b","b","a","a")
q=c("v1","v2","v1","v2","v1","v2")
r=c(5,2,4,8,9,7)
base=data.frame(p,q,r)
base
p q r
1 d v1 5
2 d v2 2
3 b v1 4
4 b v2 8
5 a v1 9
6 a v2 7
How programming R to get this result:
v1 v2
a 9 7
b 4 8
d 5 2
I tried the function table but the result is divided in several matrix:
table(base$p,base$q,base$r)
, , = 2
v1 v2
a 0 0
b 0 0
d 0 1
, , = 4
v1 v2
a 0 0
b 1 0
d 0 0
, , = 5
v1 v2
a 0 0
b 0 0
d 1 0
, , = 7
v1 v2
a 0 1
b 0 0
d 0 0
, , = 8
v1 v2
a 0 0
b 0 1
d 0 0
, , = 9
v1 v2
a 1 0
b 0 0
d 0 0
SIncerely yours
--
View this message in context:
http://r.789695.n4.nabble.com/How-to-built-a-pivot-table-of-value-tp4651539.html
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.
____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails ______________________________________________ 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.
____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails
HI,
You can also also ?reshape()
res<-reshape(base,v.names="r",idvar="p",timevar="q",direction="wide")
?res[order(res$p),]
A.K.
----- Original Message -----
From: CE.KA <ce.kaya75 at yahoo.fr>
To: r-help at r-project.org
Cc:
Sent: Saturday, December 1, 2012 7:37 AM
Subject: [R] How to built a pivot table of value
Hi R users
Imagine the table "base":
p=c("d","d","b","b","a","a")
q=c("v1","v2","v1","v2","v1","v2")
r=c(5,2,4,8,9,7)
base=data.frame(p,q,r)
base
? ? p? q? ? r
1? d? v1? 5
2? d? v2? 2
3? b? v1? 4
4? b? v2? 8
5? a? v1? 9
6? a? v2? 7
How programming R to get this result:
? ? ? ? ? ? v1? v2
a? ? ? ? ? 9? ? ? ? 7
b? ? ? ? 4? ? ? ? 8
d? ? ? ? 5? ? ? ? 2
I tried the function table but the result is divided in several matrix:
table(base$p,base$q,base$r)
, ,? = 2
?
? ? v1 v2
? a? 0? 0
? b? 0? 0
? d? 0? 1
, ,? = 4
?
? ? v1 v2
? a? 0? 0
? b? 1? 0
? d? 0? 0
, ,? = 5
?
? ? v1 v2
? a? 0? 0
? b? 0? 0
? d? 1? 0
, ,? = 7
?
? ? v1 v2
? a? 0? 1
? b? 0? 0
? d? 0? 0
, ,? = 8
?
? ? v1 v2
? a? 0? 0
? b? 0? 1
? d? 0? 0
, ,? = 9
?
? ? v1 v2
? a? 1? 0
? b? 0? 0
? d? 0? 0
SIncerely yours
--
View this message in context: http://r.789695.n4.nabble.com/How-to-built-a-pivot-table-of-value-tp4651539.html
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.