Hello, i have the following list strsplit(as.character(Elecciones$Municipios),"\\.") [[1]] [1] "ANTIOQUIA" "ABEJORRAL" [[2]] [1] "META" "ACACIAS" [[3]] [1] "CASANARE" "AGUAZUL" [[4]] and I would like to make a vector of the first element of each of the list items, in this case ANTIOQUIA, META, CASANARE, etc . Do you know how can I do this? Thank you Felipe Parra [[alternative HTML version deleted]]
Try this: sapply(strsplit(as.character(Elecciones$Municipios),"\\."), '[[', 1) On Wed, May 12, 2010 at 6:18 PM, Luis Felipe Parra < felipe.parra@quantil.com.co> wrote:> Hello, i have the following list > strsplit(as.character(Elecciones$Municipios),"\\.") > [[1]] > [1] "ANTIOQUIA" "ABEJORRAL" > [[2]] > [1] "META" "ACACIAS" > [[3]] > [1] "CASANARE" "AGUAZUL" > [[4]] > and I would like to make a vector of the first element of each of the list > items, in this case ANTIOQUIA, META, CASANARE, etc . Do you know how can I > do this? > > Thank you > > Felipe Parra > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On May 12, 2010, at 5:18 PM, Luis Felipe Parra wrote:> Hello, i have the following list > strsplit(as.character(Elecciones$Municipios),"\\.") > [[1]] > [1] "ANTIOQUIA" "ABEJORRAL" > [[2]] > [1] "META" "ACACIAS" > [[3]] > [1] "CASANARE" "AGUAZUL" > [[4]] > and I would like to make a vector of the first element of each of > the list > items, in this case ANTIOQUIA, META, CASANARE, etc . Do you know how > can I > do this? >> unlist( lapply(ll, "[", 1)) [1] "ANTIOQUIA" "META" "CASANARE"> Thank you > > Felipe ParraDavid Winsemius, MD West Hartford, CT
The key is that "[" is a function. So, you have a list, you want to apply a function to a list, and return a vector, think sapply. sapply(strsplit(as.character(Elecciones$Municipios),"\\."), "[", 1) Luis Felipe Parra wrote:> Hello, i have the following list > strsplit(as.character(Elecciones$Municipios),"\\.") > [[1]] > [1] "ANTIOQUIA" "ABEJORRAL" > [[2]] > [1] "META" "ACACIAS" > [[3]] > [1] "CASANARE" "AGUAZUL" > [[4]] > and I would like to make a vector of the first element of each of the list > items, in this case ANTIOQUIA, META, CASANARE, etc . Do you know how can I > do this? > > Thank you > > Felipe Parra > > [[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.