gpt4 book ai didi

R 突变(数据帧与 Tibble)

转载 作者:行者123 更新时间:2023-12-04 10:29:05 27 4
gpt4 key购买 nike

使用 R 3.6.1 (64) 位。使用 readxl 将数据框导入 R(命名为“RawShift”。我创建了 6 个变量(类:“字符”),它们是用户名列表。每个列表都以用户所在的团队命名。

我想使用 Mutate 创建一个列,其中包含用户所在的团队。

INTeam = C("user1", "user2",...)
OFTeam = C("user3", "user4",...)

当我使用 Data frame 时,此代码有效:
RawShift <- RawShift %>% mutate(Team =case_when(
`username` %in% OFTeam ~ "Office",
`username` %in% INTeam ~ "Industrial"
))

现在我已经在我的 Raw Shift 上完成了“as_tibble”,它不会出错,也不会工作。这是不了解 Tibble 访问方法 ("", [], ., [[]]) 的情况。是否值得担心或只是做一个黑客工作并使用数据框进行转换,然后再转换为小标题?我已经研究了 Tibble 相对于数据框的好处,在我看来,我最好使用 Tibbles,但似乎无法让它发挥作用。曾尝试使用“$”、“.”到目前为止,在 %in% 之前没有运气等。感谢您的任何建议/帮助。

最佳答案

我们可能需要加载 tibble包裹

library(dplyr)
library(tibble)
head(iris) %>%
as_tibble %>%
mutate(new = case_when(Species == "setosa" ~ "hello"))
# A tibble: 6 x 6
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
# <dbl> <dbl> <dbl> <dbl> <fct> <chr>
#1 5.1 3.5 1.4 0.2 setosa hello
#2 4.9 3 1.4 0.2 setosa hello
#3 4.7 3.2 1.3 0.2 setosa hello
#4 4.6 3.1 1.5 0.2 setosa hello
#5 5 3.6 1.4 0.2 setosa hello
#6 5.4 3.9 1.7 0.4 setosa hello

关于R 突变(数据帧与 Tibble),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60494608/

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