r - R 变异(Dataframe 与 Tibble)
问题描述
在 R 3.6.1 (64) 位中工作。使用 readxl 将数据框放入 R 中(命名为“RawShift”。我制作了 6 个变量(类:“字符”),它们是用户名列表。每个列表都以用户所在的团队命名。
我想使用 Mutate 创建一个包含用户所在团队的列。
INTeam = C("user1", "user2",...)
OFTeam = C("user3", "user4",...)
当我一直在使用数据框时,此代码有效:
RawShift <- RawShift %>% mutate(Team =case_when(
`username` %in% OFTeam ~ "Office",
`username` %in% INTeam ~ "Industrial"
))
现在我已经在我的 Raw Shift 上完成了“as_tibble”,它不会出错,也不会工作。这是不了解 Tibble 访问方法(“”、[]、.、[[]])的情况。是否值得担心或只是做一个黑客工作并使用数据框进行转换,然后再转换为小标题?我已经研究了 Tibbles 相对于数据框的好处,在我看来,使用 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
推荐阅读
- php - 如何在 php curl 中正确发布 id 和 content_no 的值
- android - Android Studio - Google Maps API 适用于调试版本,但不适用于发布版本
- android - 为什么我没有从我的 startActivityForResult 得到结果?
- php - 数组的 print_r 不打印值
- amazon-s3 - AWS s3“放置对象 - 复制”是否异步执行?
- java - Spring拦截授权端点的http请求
- ansible - 防止在 Ansible 中将变量转换为布尔值
- javascript - 从一个 html 文档转到另一个 html 文档时,我需要动态创建元素
- node.js - 如何使用 Node JS 将经过身份验证的用户传递给新视图
- c# - 我怎样才能加快这个“mini-git”课程的速度?