我一直在努力提供數據的文本分析。通常分析包括在紙上編寫一個抄本,然後將這些信息作爲數字代碼導入到R中。我想輸出一個單詞的抄本,並將他們的單詞編號上面切到某一行寬(讓我們使用任意80個字符)。對齊和交替字符串
一個最小的可視化例子:
#what we start with:
person text word.num
1 greg The 1
2 greg dog 2
3 greg went 3
4 greg to 4
5 greg the 5
6 greg zoo, 6
7 greg but 7
8 greg ate 8
9 greg first. 9
10 sally He 10
11 sally likes 11
12 sally water 12
13 sally a 13
14 sally bit 14
15 sally too. 15
#what我想:
1 2 3 4 5 6
The dog went to the zoo,
7 8 9 10 11
but ate first. He likes
12 13 14 15
water a bit too.
另外一個問題出現的號碼來獲得在大型較大的字數可能超過簡短的單詞和單詞需要在它前面放置一個額外的空間。我認爲在粘貼過程中,通過確定最大數字的最大字符(數字)並在比這個數量更少的單詞之後增加那麼多空間是很容易的。
我的想法來解決這個迄今爲止是:
- 創建每行一定的最大長度的特徵向量1列矩陣(
strwrap
可以在這裏有用) - 添加多餘的空格如上所述(
nchar
和gsub
在此處可能有用) - 通過使用字數功能來確定伴隨矩陣的數值,然後使用
cumsum
和seq
來製作伴隨矩陣的數字c值(實際上是字符)也是1列。這將匹配行與字符(單詞)矩陣的行。 - 現在兩個矩陣需要按行交替行(不知道如何做到這一點)
- 對準上面的字號碼(不知道如何做到這一點,但
nchar
可能是有用的在這裏)
我想保留這個基礎工具,雖然我敢肯定Hadely的stringR
會很有用,我想避免這種依賴。
dput
以上數據:
dat <- structure(list(person = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("greg", "sally"), class = "factor"),
text = structure(c(10L, 5L, 14L, 11L, 9L, 15L, 4L, 2L, 6L,
7L, 8L, 13L, 1L, 3L, 12L), .Label = c("a", "ate", "bit",
"but", "dog", "first.", "He", "likes", "the", "The", "to",
"too.", "water", "went", "zoo,"), class = "factor"), word.num = 1:15), row.names = c(NA,
-15L), .Names = c("person", "text", "word.num"), class = "data.frame")
我不能想出我覺得拍攝,同時可搜索的未來,使用戶的思想的標題。請提出修改建議...
是'dput'不看就像你所表現的「我們的開始」 – GSee
我的歉意。生病。儘快修復。 –
「喜歡」這個詞是不是有理由輸出? – GSee