How to get the first 10 words in a string in R?

后端 未结 4 1210
清歌不尽
清歌不尽 2020-12-17 00:00

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 specifi

4条回答
  •  旧时难觅i
    2020-12-17 00:03

    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
    

提交回复
热议问题