I have reviewed Create new character column in R data frame based on existing character vector,How to create a data frame with numeric and character columns?,Creating a new column of character values based on numeric value condition in R?, and others. From those answers, I was able to get my data frame to partially work, but I must be missing something because it's still incorrect.
Dataframe
assoc Transect year month SpeciesPositionGroupsize
<lgl> <fctr> <dbl><fctr><fctr> <chr> <dbl>
FALSE C3 2 Jan RC 470 13
FALSE C3 2 Jan BW 1850 3
TRUE C3 2 Jan RC 2020 12
TRUE C3 2 Jan BW 2020 5
FALSE C3 2 Jan SKS 1000 4
FALSE C3 2 Jan SKS 1310 3
I am trying to create a column called "habitat" based on the "position" column.
I tried
df.clean$habitat <- ifelse("Position">=2000, "interior",
ifelse("Position"<2000, "edge"))
but all that did was create a column that listed everything as "interior".
Same with this attempt
df %>%
mutate(habitat = case_when("Position" > 2000 ~ "interior",
"Position" < 2000 ~ "edge"))
What am I missing?
Desired output
assoc Transect year month SpeciesPositionGroupsize Habitat
<lgl> <fctr> <dbl><fctr><fctr> <chr> <dbl> <chr>
FALSE C3 2 Jan RC 470 13 edge
FALSE C3 2 Jan BW 1850 3 edge
TRUE C3 2 Jan RC 2020 12 interior
TRUE C3 2 Jan BW 2020 5 interior
FALSE C3 2 Jan SKS 1000 4 edge
FALSE C3 2 Jan SKS 1310 3 edge
Dput data
structure(list(assoc = c(FALSE, FALSE, TRUE, TRUE, FALSE, FALSE
), Transect = structure(c(1L, 1L, 1L, 1L, 1L, 1L), levels = c("C3",
"Msolwa", "Mwani", "Sanje"), class = "factor"), year = c(2, 2,
2, 2, 2, 2), month = structure(c(1L, 1L, 1L, 1L, 1L, 1L), levels = c("Jan",
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec"), class = "factor"), Species = structure(c(4L, 2L,
4L, 2L, 6L, 6L), levels = c("BABO", "BW", "Mang", "RC", "RD",
"SKS"), class = "factor"), Position = c("470", "1850", "2020",
"2020", "1000", "1310"), Groupsize = c(13, 3, 12, 5, 4, 3)), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))