Colleagues,
I have some text:
TEXT <- c("a", "bb;ccc", "dddd;eeeee;ffffff")
I want to retrieve the portion of each element before the first
semicolon. I can split each element using strsplit:
SPLIT <- strsplit(TEXT, ";")
This yields:
> SPLIT
[[1]]
[1] "a"
[[2]]
[1] "bb" "ccc"
[[3]]
[1] "dddd" "eeeee" "ffffff"
Now I need to access the [1] portions of [[n]] - ("a", "bb",
"dddd"). I suspect that sapply is the correct tool here but the
syntax eludes me. Can anyone direct me to the correct syntax?
Dennis
Dennis Fisher MD
P < (The "P Less Than" Company)
Phone: 1-866-PLessThan (1-866-753-7784)
Fax: 1-415-564-2220
www.PLessThan.com
[[alternative HTML version deleted]]
Try: sapply(strsplit(TEXT, ';'), '[', 1) On Fri, Apr 11, 2008 at 8:12 AM, Dennis Fisher <fisher@plessthan.com> wrote:> Colleagues, > > I have some text: > TEXT <- c("a", "bb;ccc", "dddd;eeeee;ffffff") > > I want to retrieve the portion of each element before the first > semicolon. I can split each element using strsplit: > > SPLIT <- strsplit(TEXT, ";") > > This yields: > > SPLIT > [[1]] > [1] "a" > > [[2]] > [1] "bb" "ccc" > > [[3]] > [1] "dddd" "eeeee" "ffffff" > > Now I need to access the [1] portions of [[n]] - ("a", "bb", > "dddd"). I suspect that sapply is the correct tool here but the > syntax eludes me. Can anyone direct me to the correct syntax? > > Dennis > > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone: 1-866-PLessThan (1-866-753-7784) > Fax: 1-415-564-2220 > www.PLessThan.com > > > [[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]]
check this:
TEXT <- c("a", "bb;ccc", "dddd;eeeee;ffffff")
SPLIT <- strsplit(TEXT, ";")
sapply(SPLIT, "[", 1)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Dennis Fisher" <fisher at plessthan.com>
To: <r-help at r-project.org>
Sent: Friday, April 11, 2008 1:12 PM
Subject: [R] strsplit and sapply
> Colleagues,
>
> I have some text:
> TEXT <- c("a", "bb;ccc",
"dddd;eeeee;ffffff")
>
> I want to retrieve the portion of each element before the first
> semicolon. I can split each element using strsplit:
>
> SPLIT <- strsplit(TEXT, ";")
>
> This yields:
> > SPLIT
> [[1]]
> [1] "a"
>
> [[2]]
> [1] "bb" "ccc"
>
> [[3]]
> [1] "dddd" "eeeee" "ffffff"
>
> Now I need to access the [1] portions of [[n]] - ("a",
"bb",
> "dddd"). I suspect that sapply is the correct tool here but the
> syntax eludes me. Can anyone direct me to the correct syntax?
>
> Dennis
>
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-415-564-2220
> www.PLessThan.com
>
>
> [[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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Here is a command I wrote to get the part of the email address before the @ sign. It works, but I don't know why. The variable called email is a vector of email addresses (not yet in character form, so that is why I need "as character"). names(Score) <- sapply(email,function(x) (strsplit(as.character(x),"@")[[1]]))[1,] On 04/11/08 04:12, Dennis Fisher wrote:> Colleagues, > > I have some text: > TEXT <- c("a", "bb;ccc", "dddd;eeeee;ffffff") > > I want to retrieve the portion of each element before the first > semicolon. I can split each element using strsplit: > > SPLIT <- strsplit(TEXT, ";") > > This yields: > > SPLIT > [[1]] > [1] "a" > > [[2]] > [1] "bb" "ccc" > > [[3]] > [1] "dddd" "eeeee" "ffffff" > > Now I need to access the [1] portions of [[n]] - ("a", "bb", > "dddd"). I suspect that sapply is the correct tool here but the > syntax eludes me. Can anyone direct me to the correct syntax? > > Dennis > > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone: 1-866-PLessThan (1-866-753-7784) > Fax: 1-415-564-2220 > www.PLessThan.com > > > [[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.-- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron Editor: Judgment and Decision Making (http://journal.sjdm.org)