R:如何显示一串单词的前n个字符

R: how to display the first n characters from a string of words

我有以下字符串:

 Getty <- "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all  men are created equal."

我想显示前10个字符。所以我首先将字符串拆分为单个字符:

 split <- strsplit(Getty, split="")
 split 

我得到了所有的单字。然后我创建前 10 个字符的子字符串。

 first.10 <- substr(split, start=1, stop=10)
 first.10

这是输出:

 "c(\"F\", \"o\""

我不明白为什么打印出来?我以为它只会打印出类似的东西:

 "F" "o" "u" "r" "s" 

有什么方法可以改变我的代码来打印上面的内容吗?

谢谢大家!

你得到 "c(\"F\", \"o\"" 的原因是因为 strsplit 输出是 list。我们可以通过提取第一个 list 元素将 list 转换为 vector 即。 [[1]]。使用 head 获取前 10 个字符。

head(strsplit(Getty, '')[[1]], 10)

更新

如果你只想提取没有空格的字符,

library(stringr)
head(str_extract_all(Getty, '[^ ]')[[1]],10)
#[1] "F" "o" "u" "r" "s" "c" "o" "r" "e" "a"

把你的代码反过来,你就会得到你想要的。

Getty <- "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all  men are created equal."


first.10 <- substr(Getty, start=1, stop=10)
first.10
"Four score"
split <- strsplit(first.10, split="")
split 
"F" "o" "u" "r" " " "s" "c" "o" "r" "e"

其他答案没有像您在示例中所做的那样消除空格,因此我将添加:

strsplit(substr(gsub("\s+", "", Getty), 1, 10), '')[[1]]
#[1] "F" "o" "u" "r" "s" "c" "o" "r" "e" "a"