2

I'm trying to run googleLanguageR to translate a certain column using NL API translation service. Here's my code: https://cran.r-project.org/web/packages/googleLanguageR/googleLanguageR.pdf

gl_translate(
data$comments,
target = "en",
format = c("text"),
source = "")

The output looks like this. I'd like to take the translatedText output and append it as a NEW column to my existing tibble called "data".

How can I accomplish this? Thanks. enter image description here

AdilK
  • 567
  • 1
  • 7
  • 25
  • I removed my answer as I overlooked the three columns. `cbind` is probably shorter than [other solutions](https://stackoverflow.com/questions/29614849/dplyrmutate-to-add-multiple-values) – Waldi Mar 21 '21 at 22:59

2 Answers2

1

Before running the gl_translate function, create an object called 'translation' :

translation <- 
  gl_translate(
    data$comments,
    target = "en",
    format = c("text"),
    source = "")

Bind the two data sources

cbind(data,translation)
Waldi
  • 39,242
  • 6
  • 30
  • 78
AdilK
  • 567
  • 1
  • 7
  • 25
0

Another way I found to do it, which is cheaper in terms of Google's translation costs and can do within pipes

df <- data %>%
    filter(detectedSourceLang != "en") %>%
    pull(text) %>%
    gl_translate(.,
                 target = "en",
                 source = "fr # or leave it empty
    ) %>%
    right_join(data, by = c("text" = "text"))
Nick
  • 417
  • 4
  • 14