简体   繁体   English

如何使用R分隔句子中的字母

[英]How to separate out letters in a sentence using R

I have a character vector that is a string of letters and punctuation. 我有一个字符向量,它是一串字母和标点符号。 I want to create a data frame where each column is made up of a letter/character from this string. 我想创建一个数据框,其中每一列都由该字符串中的字母/字符组成。

eg 例如

 Character string = I WENT TO THE FAIR

 Dataframe = | I |   | W | E | N | T |   | T | O |   | T | H | E |   | F | A | I | R |

I thought I could do this using a loop with substr, but I can't work out how to get R to write into separate columns, rather than just writing over the previous letter. 我以为可以使用带substr的循环来完成此操作,但是我无法弄清楚如何使R写入单独的列,而不仅仅是覆盖前一个字母。 I'm new to writing loops etc so struggling a bit to get my head around the way in which to compose what I need. 我是编写循环等的新手,所以我有点挣扎,无法理解编写所需内容的方式。

Thanks for any help and advice that you can offer. 感谢您提供的任何帮助和建议。

Best wishes, 最好的祝愿,

Natalie 娜塔莉(Natalie)

This should get that result 这应该得到那个结果

string <- "I WENT TO THE FAIR"
df <- as.data.frame(t(as.data.frame(strsplit(string,""))), row.names = "1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM