Ranji Raj
Ranji Raj

Reputation: 818

Error while conditionally adding columns to an existing dataframe

I am trying to conditionally add a column to a data frame. I have a column named comments which has a text-based on English and German languages. I basically want to detect the language and add a new column with a value en or de depending on the translation.

library(googleLanguageR)
df$language <- ifelse(df$comments %>% 
                           gl_translate_detect() %>% 
                           pull(language) == "en","en", "de"
                         )

However, I get

Error: NA/NaN argument

Expected:

title | comments        | language  
-----------------------------------
A     | I like...       |   en  
B     | I wish...       |   en  
C     | Das ist....     |   de  
C     | Es war...       |   de  
C     | Most of the...  |   en  
...

EDIT: output of gl_translate_detect()

A tibble:1 x 4
confidence  isReliable  language  text
<dbl>         <lgl>     <chr>     <chr>
0.983067      FALSE       de    Dem  ich mich. 

Upvotes: -1

Views: 36

Answers (1)

jkuo_ut
jkuo_ut

Reputation: 131

Are you open to using other packages? I cannot use googleLanguageR on my PC. But its documentation suggests using the package cld2.

From the documentation

Consider using library(cld2) and cld2::detect_language instead offline, since that is free and local without needing a paid API call.

https://cran.r-project.org/web/packages/googleLanguageR/googleLanguageR.pdf (pg.15)

# install.packages("cld2")
x = cld2::detect_language("katten sidder på måtten")
y = cld2::detect_language("how are you")
x
y
> x
[1] "da"
> y
[1] "en"

Upvotes: 1

Related Questions