'R: Problems when calling a function within mutate()
I have a function to calculate a frequency value for specific strings. Unfortunately, I failed to apply this function within mutate():
# Create a data.frame with some sample texts
df <- data.frame(var1 = c("one short text", "another example", "something", "agsjehjr", "ajase"),
language = c("D", "D", "I", "I", "D"))
# Create a lookup-table with values for two-character-combinations
lookup_table <- data.frame(token = c("ex", "te", "th", "id"),
value = c(0.2, 0.2, 0.3, 0.25),
language = c("D", "D", "I", "I"))
# A function to calculate a value for a specific word
lookup_text <- function(text, language = "D"){
df <- data.frame(token = tokens_char(text, window = 2)) %>%
mutate(language = language) %>%
left_join(lookup_table, by = c("token", "language")) %>%
mutate(value = ifelse(is.na(value), 0, value))
return(mean(df$value, na.rm=TRUE))
}
# Check: This works as expected
lookup_text("example", "D")
# This doesn't work, what am I doing wrong?
df %>%
mutate(new_var = lookup_text(var1, language = "D"))
Unfortunately, I get the same value for all texts. There's a warning message:
Warning messages:
1: Problem while computing `new_var = lookup_text(var1, language = "D")`.
i the condition has length > 1 and only the first element will be used
2: Problem while computing `new_var = lookup_text(var1, language = "D")`.
i first element used of 'length.out' argument
3: Problem while computing `new_var = lookup_text(var1, language = "D")`.
i longer object length is not a multiple of shorter object length
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
