0
votes

So I am trying to count the number of characters in each word. I have a .db file. with 5 variables and one has 100 sets of words. Here is my code

library(DBI)
library(RSQLite)
library(dplyr)

texts <- dbReadTable(con, "Document")
char <- texts %>% select(words)
str_count(char)

But my output is "argument is not an atomic vector; coercing[1] 563" so the total number of 563 characters but what I want is a list/column were like word 1 has 4 characters, word 2 has 7, word 3 has 2... etc. Any ideas?

1
The problem is because select returns a data.frame or tbl_df, whereas you are expecting it to be a vector. Try pull(texts, words), or if you must have the pipe, then texts %>% pull(words) or just the boring-old texts$words. Or, as Konrad suggested, str_count(texts$words) or even just nchar(texts$words) (base R).r2evans

1 Answers

1
votes

As the error message says, str_count expects a character vector. You’re passing it a data.frame.

To fix this, pass it the words column of char:

str_count(char$words)

Or, more likely, you want to modify your table and add a column with the counts:

char <- texts %>%
    select(words) %>%
    mutate(length = str_count(words))