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]]
Nico, It is unclear to me what sort of result you are looking for. Can you give an example of what the output would look like using the example data you provided? You can look at the occurrence of names and places using the table() function, table(a$name, a$place) or you could look at the mean value for each name/place combination using the tapply() function, tapply(a$value, list(a$name, a$place), mean) Jean On Mon, Jun 3, 2013 at 8:41 AM, Nico Met <nicomet80@gmail.com> wrote:> 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@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]]
Hi, It is not clear. 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) join_all(lapply(lstdta2,`[`,-1),by="name",type="inner") #none of them are common #[1] name? value value value value value #<0 rows> (or 0-length row.names) A.K. ----- Original Message ----- From: Nico Met <nicomet80 at gmail.com> To: R help <r-help at r-project.org> Cc: Sent: Monday, June 3, 2013 9:41 AM 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.
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.dfNote 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.dfNow 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.