[R] split and common variables
David Carlson
dcarlson at tamu.edu
Wed Jun 5 17:46:07 CEST 2013
Your request is still vague. Arun has given you one approach. This
will tell you which names are found in pairs of places:
> ndf <- length(names(dta.df.sp))
> results <- vector("list")
> for (i in 1:(ndf-1)) {
+ for (j in (i+1):ndf) {
+ pair <- paste(names(dta.df.sp)[i], names(dta.df.sp)[j])
+ results[[pair]] <- intersect(dta.df.sp[[i]]$name,
dta.df.sp[[j]]$name)
+ k <- k+1
+ }
+ }
> results
$`GCKT IKLI`
character(0)
$`GCKT KLOI`
[1] "P_CK24" "P_CK29" "P_CK32"
$`GCKT PLRT`
[1] "P_CK33" "P_CK30"
$`GCKT POIV`
[1] "P_CK24" "P_CK33" "P_CK26"
$`IKLI KLOI`
[1] "P_CK25"
$`IKLI PLRT`
[1] "P_CK23" "P_CK25"
$`IKLI POIV`
[1] "P_CK23" "P_CK28" "P_CK31"
$`KLOI PLRT`
[1] "P_CK27" "P_CK25"
$`KLOI POIV`
[1] "P_CK24"
$`PLRT POIV`
[1] "P_CK23" "P_CK33"
-------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Wednesday, June 5, 2013 10:37 AM
To: Nico Met
Cc: dcarlson at tamu.edu; R help
Subject: Re: [R] split and common variables
Hello,
May be this helps:
dta1<-do.call(data.frame,dta)
dta2<-dta1[complete.cases(dta1),]
dta2[,-3]<-lapply(dta2[,-3],as.character)
lstdta2<-split(dta2,dta2$place)
library(plyr)
#Some names are common in a few, but not in all the places. If you
are looking for names common in 2 places, 3, places, etc...
lstNew<-lapply(2:5,function(i){x1<-
combn(names(lstdta2),i);lapply(split(x1,col(x1)),function(x){x2<-pas
te(x,collapse="_");lst1<-lapply(lstdta2[x],`[`,-1);lst2<-join_all(ls
t1,by="name",type="inner");names(lst2)<-x2;
colnames(lst2)[-1]<-paste("value",1:(ncol(lst2)-1),sep="");lst2}) })
lstNew1<-lapply(lstNew,function(x) x[lapply(x,nrow)!=0])
lstNew2<-lstNew1[lapply(lstNew1,length)!=0]
lstNew2[[1]][1:2]
#$`2`
# GCKT_KLOI value1 value2
#1 P_CK24 6.0786 -1.2738
#2 P_CK29 1.2757 -13.7960
#3 P_CK29 1.2757 -34.8020
#4 P_CK32 -8.1963 -11.1280
#5 P_CK32 -8.1963 -35.9044
#
#$`3`
# GCKT_PLRT value1 value2
#1 P_CK33 6.2826 -14.4522
#2 P_CK33 6.2826 -9.4303
#3 P_CK30 15.3869 -23.9914
#4 P_CK30 15.3869 -11.1103
A.K.
----- Original Message -----
From: Nico Met <nicomet80 at gmail.com>
To: dcarlson at tamu.edu
Cc: R help <r-help at r-project.org>
Sent: Wednesday, June 5, 2013 10:30 AM
Subject: Re: [R] split and common variables
Dear Dr. David,
Many thanks for your answer.
Now, if I want to see if there are common "name"in those "places" ,
how can
I do it? So, by common I mean, it might be compared with 2, 3, 4
.... all
of them. All possible combinations
Many thanks
regards
Nico
On Mon, Jun 3, 2013 at 5:20 PM, David Carlson <dcarlson at tamu.edu>
wrote:
> It may be easier if you convert the list you provided to a
> data.frame:
>
> > dta.df <- data.frame(place=dta$place, name=dta$name,
> value=dta$value)
> > dta.df
>
> Note that the last line is blank so you probably want to remove
that
> and remove the blank factor levels:
>
> > dta.df <- dta.df[-nrow(dta.df),]
> > dta.df$place <- factor(dta.df$place)
> > dta.df$name <- factor(dta.df$name)
> > dta.df
>
> Now you can make a list containing separate data.frames by place
>
> > dta.df.sp <- split(dta.df, dta.df$place)
> > dta.df.sp
>
> -------------------------------------
> David L Carlson
> Associate Professor 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 Nico Met
> Sent: Monday, June 3, 2013 8:42 AM
> To: R help
> Subject: [R] split and common variables
>
> Dear all,
>
> I would like to split the data based on the "place" and then would
> like to
> see how many "names" were common in place with corresponding
"value"
> .
>
> Please find a demo file.
>
> Thanks for your expert comment
>
> best
>
> Nico
>
> > dput(dta)
> structure(list(place = structure(c(3L, 2L, 5L, 6L, 4L, 3L, 2L,
> 5L, 6L, 4L, 2L, 5L, 6L, 4L, 2L, 5L, 6L, 4L, 2L, 3L, 2L, 5L, 6L,
> 4L, 3L, 2L, 5L, 6L, 4L, 5L, 6L, 4L, 5L, 6L, 1L), .Label = c("",
> "GCKT", "IKLI", "KLOI", "PLRT", "POIV"), class = "factor"), name =
> structure
> (c(2L,
> 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 2L, 3L, 4L, 5L, 6L,
> 7L, 8L, 9L, 10L, 11L, 12L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
> 11L, 12L, 12L, 1L), .Label = c("", "P_CK23", "P_CK24", "P_CK25",
> "P_CK26", "P_CK27", "P_CK28", "P_CK29", "P_CK30", "P_CK31",
> "P_CK32",
> "P_CK33"), class = "factor"), value = c(5.9464, 6.0786, -4.5155,
> 15.0241, -38.1847, -0.0861, 1.2757, -23.9914, 9.5951, -11.128,
> 6.2826, 23.5218, 20.862, 3.1626, 6.242, -20.5348, -14.0126,
-13.796,
> 15.3869, -15.7409, -8.1963, -14.4522, 3.2117, -1.2738, 14.3556,
> -12.5337, 20.4308, -3.3227, -34.802, -11.1103, -9.7146, -35.9044,
> -9.4303, -28.1949, NA)), .Names = c("place", "name", "value"),
class
> = "data
> .frame", row.names = c(NA,
> 35L))
> >
>
> [[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.
>
>
[[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.
More information about the R-help
mailing list