如何获得R中的字符串中的前10个单词? [英] How to get the first 10 words in a string in R?

查看:1392
本文介绍了如何获得R中的字符串中的前10个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个字符串

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

我想要上面指定的字符串的前10个字。

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

例如,我有一个CSV文件,格式如下: -

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

只从每行的关键字列中获取前10个字,并将其写入CSV文件。
请在这方面帮助我。

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.

推荐答案

这里是一个小的函数unlist字符串,子集前十然后将其粘贴在一起。

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)

$ b b

EDIT
这可能是一个更常用的方法。

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

这篇关于如何获得R中的字符串中的前10个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆