[R] make dataframe from table
Henrique Dallazuanna
wwwhsd at gmail.com
Mon Feb 4 16:19:13 CET 2008
Perhaps:
affect <- as.data.frame(do.call('rbind', tapply(data$V2, data$V1, table)))
merge(age, affect, by.x=1, by.y=0)
On 04/02/2008, Boks, M.P.M. <M.P.M.Boks at umcutrecht.nl> wrote:
> Dear R-experts,
>
>
> I have got a dataframe:
> data
> ID disease
> V1 V2
> 1 p1 1
> 2 p1 3
> 3 p3 3
> 4 p3 5
> 5 p5 1
>
> >From which I extract a usefull table: affect
>
> affect
> 1 3 5
> p1 1 1 0
> p3 0 1 1
> p5 1 0 0
>
> I want to merge this with anotherdataframe:
> age
> p1 23
> p2 24
> p3 23
> p4 11
> p5 45
>
> If have tried as.data.frame(affect) and other solutions to get the
> following comment going:
> merge(age,affect, by.x=0, by.y=1)
>
> QUESTION: I can get the merging process going with the outcome of the
> table command.
>
> Any help would be great.
>
> R for windows v2.6.1
>
> code:
>
> data<-as.data.frame(matrix(c("p1","p1","p3","p3","p5",1,3,3,5,1),5,2))
> age<-as.data.frame(matrix(c("p1","p2","p3","p4","p5",23,24,23,11,45),5,2
> ))
> affect<-table(data[,1],data[,2])
> merge(age,affect, by.x=0, by.y=1)
>
>
> BW
>
> Marco
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
More information about the R-help
mailing list