我需要理解开发一个可以计算字符串中第二个和第三个单词的字符数的代码。
我得到了这段代码,但是它只适用于第一个单词的字符数。
现在我被允许使用Spark SQL或dplyr软件包。
这是我为计算第一个单词中的字符而做的
INSTR(NAME_NORM_LONG,' ')-1)
预期结果是对字符进行计数并将结果显示在新列中。
word="hey I am Scott"
characters_word1 | characters_word2 | characters_word3
3 1 2
现在,我正在运行以下代码进行测试(尝试查找第二个单词):
test_query<-test_query %>%
mutate(Total_char=nchar(NAME_NORM_LONG))%>%
mutate(Name_has_numbers=str_detect(NAME_NORM_LONG,"[[:digit:]]"))%>%
mutate(number_words=LENGTH(NAME_NORM_LONG) - LENGTH(REPLACE(NAME_NORM_LONG, ' ', '')) + 1)%>%
mutate(number_chars_w1=INSTR(NAME_NORM_LONG,' ')-1)%>%
mutate(number_chars_w2=substr(NAME_NORM_LONG,number_chars_w1+1,LENGTH(NAME_NORM_LONG)))``` and the result I am having is this one ```test_query
# Source: spark<?> [?? x 7]
PBIN0 NAME_NORM_LONG Total_char Name_has_numbers number_words number_chars_w1
<int> <chr> <int> <lgl> <dbl> <dbl>
1 4.01e8 GM BUILDERS 11 FALSE 2 2
# … with 1 more variable: number_chars_w2 <chr>
Warning messages:
1: In substr(NAME_NORM_LONG, number_chars_w1, 1) :
NAs introduced by coercion
2: In substr(NAME_NORM_LONG, number_chars_w1, 1) :
NAs introduced by coercion
3: In substr(NAME_NORM_LONG, number_chars_w1, 1) :
NAs introduced by coercion
4: In substr(NAME_NORM_LONG, number_chars_w1, 1) :
NAs introduced by coercion
5: In substr(NAME_NORM_LONG, number_chars_w1, 1) :
NAs introduced by coercion```
答案 0 :(得分:1)
如何使用str_split()
?
word="hey I am Scott"
word_list = stringr::str_split(word, " ")
n = length(word_list[[1]])
for (i in 1:n){
first_row = paste0("characters_word", 1:n)
second_row = sapply(word_list[[1]], nchar)
}
df = data.frame(first_row, second_row)