To jest moje pierwsze pytanie na SO, więc daj mi znać, czy można go poprawić. Pracuję nad projektem przetwarzania języka naturalnego w R i próbuję zbudować tabelę data.table zawierającą przypadki testowe. Tutaj buduję znacznie uproszczony przykład:Podział na ciąg danych kolumna danych produkuje NAs
texts.dt <- data.table(string = c("one",
"two words",
"three words here",
"four useless words here",
"five useless meaningless words here",
"six useless meaningless words here just",
"seven useless meaningless words here just to",
"eigth useless meaningless words here just to fill",
"nine useless meaningless words here just to fill up",
"ten useless meaningless words here just to fill up space"),
word.count = 1:10,
stop.at.word = c(0, 1, 2, 2, 4, 3, 3, 6, 7, 5))
ta zwraca data.table będziemy pracować nad:
string word.count stop.at.word
1: one 1 0
2: two words 2 1
3: three words here 3 2
4: four useless words here 4 2
5: five useless meaningless words here 5 4
6: six useless meaningless words here just 6 3
7: seven useless meaningless words here just to 7 3
8: eigth useless meaningless words here just to fill 8 6
9: nine useless meaningless words here just to fill up 9 7
10: ten useless meaningless words here just to fill up space 10 5
W rzeczywistym zastosowaniu, wartości w kolumnie stop.at.word
są ustalane w sposób losowy (z górną granicą = word.count
- 1). Ponadto ciągi nie są uporządkowane według długości, ale nie powinno to mieć znaczenia.
Kod powinien dodać dwie kolumny input
i output
, gdzie input
zawiera podciąg od pozycji 1 do stop.at.word
i output
zawiera słowo to następująco (jedno słowo) tak:
>desired_result
string word.count stop.at.word input
1: one 1 0
2: two words 2 1 two
3: three words here 3 2 three words
4: four useless words here 4 2 four useless
5: five useless meaningless words here 5 4 five useless meaningless words
6: six useless meaningless words here just 6 2 six useless
7: seven useless meaningless words here just to 7 3 seven useless meaningless
8: eigth useless meaningless words here just to fill 8 6 eigth useless meaningless words here just
9: nine useless meaningless words here just to fill up 9 7 nine useless meaningless words here just to
10: ten useless meaningless words here just to fill up space 10 5 ten useless meaningless words here
output
1:
2: words
3: here
4: words
5: here
6: meaningless
7: words
8: to
9: fill
10: just
Niestety co ja zamiast dostać to:
string word.count stop.at.word input output
1: one 1 0
2: two words 2 1 NA NA
3: three words here 3 2 NA NA
4: four useless words here 4 2 NA NA
5: five useless meaningless words here 5 4 NA NA
6: six useless meaningless words here just 6 3 NA NA
7: seven useless meaningless words here just to 7 3 NA NA
8: eigth useless meaningless words here just to fill 8 6 NA NA
9: nine useless meaningless words here just to fill up 9 7 NA NA
10: ten useless meaningless words here just to fill up space 10 5 ten NA
zauważy niespójne wyniki, z pustym ciągiem w wierszu 1 i „dziesięć” zwróciło na wiersz 10.
Oto kod używam:
texts.dt[, c("input", "output") := .(
substr(string,
1,
sapply(gregexpr(" ", string),"[", stop.at.word) - 1),
substr(string,
sapply(gregexpr(" ", string),"[", stop.at.word),
sapply(gregexpr(" ", string),"[", stop.at.word + 1) - 1)
)]
Pobiegłem wiele testów oraz instrukcje substr
działa dobrze, gdy próbuję poszczególne ciągi w konsoli, ale nie po nałożeniu na data.table. Podejrzewam, że brakuje mi czegoś związanego z określaniem zakresu w data.table, ale nie używam tego pakietu przez długi czas, więc jestem bardzo zdezorientowany.
Byłbym bardzo wdzięczny za pomoc. Z góry dziękuję!
Drobne skargi: staraj się, aby Twoje przykłady były na tyle małe, że nie wymagają przewijania w przeglądarce. – Frank
@Franck - Oczywiście, następnym razem zrobię lepiej! –
Nie jestem pewien, dlaczego pozostałe dwie odpowiedzi zostały usunięte ...? @ProcrastinatusMaximus – eddi