referring to substrings

0 views
Skip to first unread message

Julia Deak

unread,
Jun 17, 2008, 2:38:17 PM6/17/08
to CorpLing with R
Hi Stefan et al! (Stefan, I was in your class at the LSA last year in
case you're wondering).

I have this object "parts" which apparently is a list because I
created it with strsplit...

> parts
[[1]]
[1] "*S001: Warum sind sie"
[2] "schlimm Deustch Student sprechen? 3:30:09 "

I want to refer to the different items [1] and [2] of [[1]] but can't
figure out how to do so. Or else convert the parts back into something
more manageable like a regular vector (as.vector doesn't seem to
change the fact that it's a list).

I'm just trying to insert tab stops around a focal word in a line, but
I couldn't get the following to work, so I tried to use strsplit and
cat but ran into the above problem.

> output.tabbed<-gsub("(\\Wmit\\W)", "\t\\1\t", output)
> head(output.tabbed)
[1] "*S001: Warum sind sie\t mit \tschlimm Deustch Student sprechen?
3:30:09 "
[2] "*S001: Warum sprechen sie\t mit \tmich? Ich denke es ist sehr
"
[3] "*S001: Wenn ich Freizeit habe, lese ich oder spreche ich\t mit
\t"

Any suggestions would be much appreciated.

Danke Schoen,
Julia Deak

Stefan Th. Gries

unread,
Jun 17, 2008, 4:58:55 PM6/17/08
to corplin...@googlegroups.com
Hi

Well, if the issue is only to put tab stops around something, then
this is probably the easiest way to go:

some.corpus<-c("This is a pretty stupid example sentence.", "And this
is probably an even more stupid example sentence.")
(with.tabs<-gsub("\\b(stupid)\\b", "\t\\1\t", some.corpus, perl=T))

Note that the second line here does not require non-word characters to
be present but just requires a word boundary, which is sometimes
better (e.g., at the end of strings when no non-word character is
following).

If you have a list of the kind you have and want to convert it to a
list, just use unlist

(some.list<-strsplit(some.corpus, "stupid"))
(vectors.again.1<-unlist(some.list))

And, since your list seems to have broken up things that belong
together, this is how you would paste it together again:

(vectors.again.2<-sapply(some.list, paste, collapse="stupid"))

HTH,
STG
--
Stefan Th. Gries
-----------------------------------------------
University of California, Santa Barbara
http://www.linguistics.ucsb.edu/faculty/stgries
-----------------------------------------------

Reply all
Reply to author
Forward
0 new messages