[R] match from a data.frame in dependence of an ID
David Carlson
dcarlson at tamu.edu
Fri Mar 28 17:45:55 CET 2014
This is a bit more direct. It works by forcing R to treat test
as a matrix rather than a table:
> tst2 <- data.frame(ID=dimnames(test)$ID,
as.data.frame.matrix(test),
check.names=FALSE)
> tst2
ID 1 2 3 4 5
10 10 10 20 30 40 50
11 11 0 0 60 70 0
12 12 0 0 0 80 0
> rownames(tst2) <- NULL
The last command simply changes the rownames from 10:12 to 1:3.
You may run into problems depending on what you are doing with
the data frame since 1, 2, etc are not valid column names. For
example:
> tst2$1
Error: unexpected numeric constant in "tst2$1"
So you might want to change to something like:
> tst2 <- data.frame(ID=dimnames(test)$ID,
G=as.data.frame.matrix(a))
> tst2
ID G.1 G.2 G.3 G.4 G.5
10 10 10 20 30 40 50
11 11 0 0 60 70 0
12 12 0 0 0 80 0
> tst2$G.1
[1] 10 0 0
>
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Rui Barradas
Sent: Friday, March 28, 2014 10:43 AM
To: Mat; r-help at r-project.org
Subject: Re: [R] match from a data.frame in dependence of an ID
Hello,
Maybe there are other ways but the following works.
tst2 <- matrix(nrow = dim(test)[1], ncol = dim(test)[2])
tst2[] <- test
tst2 <- cbind(dimnames(test)[[1]], tst2)
colnames(tst2) <- c("ID", dimnames(test)[[2]])
tst2 <- as.data.frame(tst2)
tst2
Hope this helps,
Rui Barradas
Em 28-03-2014 15:07, Mat escreveu:
> Thanks first. Your solutions works nearly perfect, i only have
one problem
> left.
>
> The result looks like perfect, my problem is now, that i want
to convert the
> solution into a data.frame.
>
> If i try
> test<-xtabs(df2$Value~df2$ID + df2$Group)
> test
> df2$Group
> df2$ID 1 2 3 4 5
> 10 10 20 30 40 50
> 11 0 0 60 70 0
> 12 0 0 0 80 0
>
> perfect.
>
> Now i try:
>
> test<-as.data.frame(test)
> test
> df2.ID df2.Group Freq
> 1 10 1 10
> 2 11 1 0
> 3 12 1 0
> 4 10 2 20
> 5 11 2 0
> 6 12 2 0
> 7 10 3 30
> 8 11 3 60
> 9 12 3 0
> 10 10 4 40
> 11 11 4 70
> 12 12 4 80
> 13 10 5 50
> 14 11 5 0
> 15 12 5 0
>
> Maybe i can delete the entries on the top "df2$Group" and
"df2$ID"?
> My solution should like this one, that i can convert into a
data.frame
>
>
> ID 1 2 3 4 5
> 1 10 10 20 30 40 50
> 2 11 0 0 60 70 0
> 3 12 0 0 0 80 0
>
> Thank you
> Mat
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/match-from-a-data-frame-in-depende
nce-of-an-ID-tp4687745p4687767.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.
>
______________________________________________
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.
More information about the R-help
mailing list