gpt4 book ai didi

r - separate_rows 在结果周围生成引号

转载 作者:行者123 更新时间:2023-12-05 08:49:33 26 4
gpt4 key购买 nike

separate_rows 在操作后围绕结果值生成引号 (")。这是正常行为吗?如何在同一操作中防止它发生而不在之后明确删除它们手术?

df <- data.frame(a = c("c_1", "c_2", "c_3", "c_4", "c_5"), 
b = c("a (+1)", "b (+2)", "a (+2), c (+5)", "e (+2)", "b (+2), e (+5)"))

a b
1 c_1 a (+1)
2 c_2 b (+2)
3 c_3 a (+2), c (+5)
4 c_4 e (+2)
5 c_5 b (+2), e (+5)

df %>% tidyr::separate_rows(b, sep = ",", convert = TRUE)
# # A tibble: 7 x 2
# a b
# <chr> <chr>
# 1 c_1 "a (+1)"
# 2 c_2 "b (+2)"
# 3 c_3 "a (+2)"
# 4 c_3 " c (+5)"
# 5 c_4 "e (+2)"
# 6 c_5 "b (+2)"
# 7 c_5 " e (+5)"

问题是not about将一行分成多行。它已经在我的尝试中显示并且代码可以实现它。

最佳答案

那些引号并不像您想象的那样存在,这正是 tidyverse 的打印方式 - 试图表明存在空白。见下文:

library(tidyverse)

x1 <- df %>% separate_rows(b, sep = ",", convert = TRUE)
x2 <- as.data.frame(x1)

x1
# # A tibble: 7 x 2
# a b
# <chr> <chr>
# 1 c_1 "a (+1)"
# 2 c_2 "b (+2)"
# 3 c_3 "a (+2)"
# 4 c_3 " c (+5)"
# 5 c_4 "e (+2)"
# 6 c_5 "b (+2)"
# 7 c_5 " e (+5)"

x2
# a b
# 1 c_1 a (+1)
# 2 c_2 b (+2)
# 3 c_3 a (+2)
# 4 c_3 c (+5)
# 5 c_4 e (+2)
# 6 c_5 b (+2)
# 7 c_5 e (+5)

identical(x1$b, x2$b)
# [1] TRUE

关于r - separate_rows 在结果周围生成引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63775925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com