R 突变(Dataframe 与 Tibble)
R Mutate (Dataframe vs Tibble)
正在使用 R 3.6.1 (64) 位。使用 readxl 将数据框导入 R(命名为 "RawShift"。我制作了 6 个变量(class:"character"),它们是用户名列表。每个列表都以该用户的团队命名已开启。
我想使用 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访问方法(“”,[],.,[[]])的情况。值得担心还是只是做一个 hack 工作并使用数据框转换然后再转换为 tibble?我研究了 Tibble 相对于 dataframe 的好处,在我看来我最好使用 Tibbles,但似乎无法正常工作。尝试过使用“$”、“.”到目前为止,在 %in% 之前没有运气等。感谢任何 advice/help.
我们可能需要加载 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 3.6.1 (64) 位。使用 readxl 将数据框导入 R(命名为 "RawShift"。我制作了 6 个变量(class:"character"),它们是用户名列表。每个列表都以该用户的团队命名已开启。
我想使用 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访问方法(“”,[],.,[[]])的情况。值得担心还是只是做一个 hack 工作并使用数据框转换然后再转换为 tibble?我研究了 Tibble 相对于 dataframe 的好处,在我看来我最好使用 Tibbles,但似乎无法正常工作。尝试过使用“$”、“.”到目前为止,在 %in% 之前没有运气等。感谢任何 advice/help.
我们可能需要加载 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