gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:22:14 26 4
gpt4 key购买 nike

我有以下字符串:

 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" 

有没有办法可以改变我的代码来打印我上面的内容?

谢谢大家!

最佳答案

扭转你的代码,你就会得到你想要的。

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"

关于R:如何显示一串单词的前n个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33199203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com