问题
I am trying to use sparklyr to process a parquet file.
the table is of structure:
type:str | type:str | type:str key | requestid | operation
I am running the code:
txt %>%
select(key, requestid, operation) %>%
mutate(object = stringr::str_split(key, '/', simplify=TRUE) %>% dplyr::last() )
where txt is a valid spark frame I get:
Error in stri_split_regex(string, pattern, n = n, simplify = simplify, : object 'key' not found
Traceback:
1. txt2 %>% select(key, requestid, operation) %>% mutate(object = stringr::str_split(key,
. "/", simplify = TRUE) %>% dplyr::last())
2. withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
3. eval(quote(`_fseq`(`_lhs`)), env, env)
4. eval(quote(`_fseq`(`_lhs`)), env, env)
5. `_fseq`(`_lhs`)
6. freduce(value, `_function_list`)
7. withVisible(function_list[[k]](value))
8. function_list[[k]](value)
9. mutate(., object = stringr::str_split(key, "/", simplify = TRUE) %>%
. dplyr::last())
10. mutate.tbl_lazy(., object = stringr::str_split(key, "/", simplify = TRUE) %>%
. dplyr::last())
11. partial_eval_dots(dots, vars = op_vars(.data))
12. lapply(dots, function(x) {
. new_quosure(partial_eval(get_expr(x), vars = vars, env = get_env(x)),
. get_env(x))
. })
13. FUN(X[[i]], ...)
14. new_quosure(partial_eval(get_expr(x), vars = vars, env = get_env(x)),
. get_env(x))
15. partial_eval(get_expr(x), vars = vars, env = get_env(x))
16. partial_eval_call(call, vars, env)
17. lapply(call[-1], partial_eval, vars = vars, env = env)
18. FUN(X[[i]], ...)
19. partial_eval_call(call, vars, env)
20. eval_bare(call, env)
21. stringr::str_split(key, "/", simplify = TRUE)
22. stri_split_regex(string, pattern, n = n, simplify = simplify,
. opts_regex = opts(pattern))
any ideas what is wrong?
回答1:
This question has more or less been addressed here.
I don't think that stringr is directly compatible with sparklyr unfortunately. But in general what you are trying to do can be solved a few ways.
- With substring commands. You can take a portion of the string that comes before your break (in this case '') and then the portion that comes after. e.g.
temp <- data.frame(
var1 = c("a_b","a_b")
,var2 = c(1,2)
)
sdf_copy_to(con,temp,"temp", overwrite = TRUE)
a <- sdf_sql(con,"select * from temp")
b <- a %>%
dplyr::mutate(var1_part1 = sql("substr(var1,1,position('_',var1)-1)")
,va1_part2 = sql("substr(var1,position('_',var1)+1,length(var1))"))
- With an array you can separate the column into a list of elements using 'split' and then make each element into a column. e.g.
a <- sdf_sql(con,"select * from temp")
b <- a %>%
dplyr::mutate(var1_array = split(var1,'_')) %>%
sdf_separate_column("var1_array", into = c("var1_part1", "var1_part2"))
来源:https://stackoverflow.com/questions/64988634/sparklyr-mutate-behaviour-with-stringr