8

I have a string in R as

x <- "The length of the word is going to be of nice use to me"

I want the first 10 words of the above specified string.

Also for example I have a CSV file where the format looks like this :-

Keyword,City(Column Header)
The length of the string should not be more than 10,New York
The Keyword should be of specific length,Los Angeles
This is an experimental basis program string,Seattle
Please help me with getting only the first ten words,Boston

I want to get only the first 10 words from the column 'Keyword' for each row and write it onto a CSV file. Please help me in this regards.

4 Answers 4

21

Regular expression (regex) answer using \w (word character) and its negation \W:

gsub("^((\\w+\\W+){9}\\w+).*$","\\1",x)
  1. ^ Beginning of the token (zero-width)
  2. ((\\w+\\W+){9}\\w+) Ten words separated by not-words.
    1. (\\w+\\W+){9} A word followed by not-a-word, 9 times
      1. \\w+ One or more word characters (i.e. a word)
      2. \\W+ One or more non-word characters (i.e. a space)
      3. {9} Nine repetitions
    2. \\w+ The tenth word
  3. .* Anything else, including other following words
  4. $ End of the token (zero-width)
  5. \\1 when this token found, replace it with the first captured group (the 10 words)
4
  • Worked perfectly for me, how to understand the regular expressions for future use? Jan 12, 2014 at 23:01
  • 1
    Why not just gsub("^((\\w+\\W+){10}).*","\\1",x) ? Jan 12, 2014 at 23:11
  • @thelatemail That includes the trailing space (if present) although the proposed method does as well if there is trailing space at the end but not 10 words total.
    – Dason
    Jan 13, 2014 at 4:05
  • 2
    But changing the regex to "^((\\w+\\W+){0,9}\\w+).*" fixes that problem too.
    – Dason
    Jan 13, 2014 at 4:18
10

How about using the word function from Hadley Wickham's stringr package?

word(string = x, start = 1, end = 10, sep = fixed(" "))

5

Here is an small function that unlist the strings, subsets the first ten words and then pastes it back together.

string_fun <- function(x) {
  ul = unlist(strsplit(x, split = "\\s+"))[1:10]
  paste(ul,collapse=" ")
}

string_fun(x)

df <- read.table(text = "Keyword,City(Column Header)
The length of the string should not be more than 10 is or are in,New York
The Keyword should be of specific length is or are in,Los Angeles
                 This is an experimental basis program string is or are in,Seattle
                 Please help me with getting only the first ten words is or are in,Boston", sep = ",", header = TRUE)

df <- as.data.frame(df)

Using apply (the function isn't doing anything in the second column)

df$Keyword <- apply(df[,1:2], 1, string_fun)

EDIT Probably this is a more general way to use the function.

df[,1] <- as.character(df[,1])
df$Keyword <- unlist(lapply(df[,1], string_fun))

print(df)
#                      Keyword                            City.Column.Header.
# 1    The length of the string should not be more than            New York
# 2  The Keyword should be of specific length is or are         Los Angeles
# 3  This is an experimental basis program string is or             Seattle
# 4      Please help me with getting only the first ten              Boston
3
  • 1
    The second line of your function could be simplified to: paste(ul,collapse=" ") Jan 13, 2014 at 0:33
  • what is the library for unlist in r?@Martin Bel Jan 13, 2014 at 0:48
  • unlist() is in base, no need to load it! Read the documentation ?unlist
    – marbel
    Jan 13, 2014 at 3:40
2
x <- "The length of the word is going to be of nice use to me"
head(strsplit(x, split = "\ "), 10)
1
  • 6
    The right idea but not quite. Try head(unlist(strsplit(x, split = "\\s+")),10) Jan 12, 2014 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.