2
votes

Working in R 3.6.1 (64) bit. Used readxl to get a data frame into R (Named "RawShift". I made 6 variables (class: "character") that are lists of user names. Each list is named for the team that user is on.

I want to use Mutate to create a column that has the team the user is from.

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

When I had been working with Data frame this code worked:

RawShift <- RawShift %>% mutate(Team =case_when(
  `username` %in% OFTeam ~ "Office",
  `username` %in% INTeam ~ "Industrial"
))

Now I've taken that and done "as_tibble" on my Raw Shift, it won't error not will it work. Is this a case of not understanding the Tibble access methods ("", [], ., [[]]). Is it worth worry about or just do a hack job and convert using data frame then convert to a tibble later? I've looked into the benefits of Tibble over dataframe and seems to me I'd be better using Tibbles but cant seem to get this working. Have tried using "$", "." etc before the %in% without luck so far. Thanks for any advice/help.

1
Can you show a small. reproducible example. It is c instead of Cakrun
Sorry not very good with Reprex yet. Tried to re-do smaller: code Data <- as_tibble(Data) # Define Teams OFTeam <- c("Scott, Andrews", "Linda, Kuang") INTeam <- c("Lee, Vanessa", "Chan, Stephen", "Hartmann, Jonathan") # Create "Team" Column in Shift Data based on Account Assessor Data <- Data %>% mutate(Team = case_when( "User Name" %in% OFTeam ~ "Office", "User Name" %in% INTeam ~ "Industrial" ))SMS
I am not clear what the issue is. The code seems to be legit. with a reproducble example 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 newakrun
When I use reprex it does give a strange error: "#> Error in as_tibble(Data): could not find function "as_tibble"" I've loaded tidyverse as well as dplyr expressly though so not really sure where its getting that and it doesnt give a console error.SMS
Yoou may have to load library(tibble)akrun

1 Answers

2
votes

We may need to load the tibble package

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