R:如何显示一串单词的前n个字符 [英] R: how to display the first n characters from a string of words
本文介绍了R:如何显示一串单词的前n个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下字符串:
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 个字符.所以我首先将字符串拆分为单个字符:
I want to display the first 10 characters. So I began by splitting the string into individual characters:
split <- strsplit(Getty, split="")
split
我得到了所有的单个字符作为这一点.然后我制作前 10 个字符的子字符串.
I get all the individual characters as this point. Then I make a substring of the first 10 characters.
first.10 <- substr(split, start=1, stop=10)
first.10
这是输出:
"c(\"F\", \"o\""
我不明白为什么会打印出来?我以为它会打印出类似的东西:
I am not understanding why this prints out? I thought it would just print out something like:
"F" "o" "u" "r" "s"
有什么办法可以改变我的代码来打印上面的内容吗?
Is there a way I can alter my code to print what I have above?
谢谢大家!
推荐答案
其他答案并没有像您在示例中那样消除空格,因此我将添加以下内容:
The other answers didn't eliminate the spaces as you did in your example, so I'll add this:
strsplit(substr(gsub("\\s+", "", Getty), 1, 10), '')[[1]]
#[1] "F" "o" "u" "r" "s" "c" "o" "r" "e" "a"
这篇关于R:如何显示一串单词的前n个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文