0

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"))
  • Your `Position` column is `character` class but you are expecting it to behave as a numeric. It seems like it should be numeric. (`<` and `>=` on character columns will use alphabetical order, meaning `"2" > "100000"` is TRUE.) Convert it with `df.clean$Position <- as.numeric(df.clean$Position)` – Gregor Thomas Mar 29 '23 at 17:00
  • Even when I convert, I still get the same issue. – Marnee Roundtree Mar 29 '23 at 17:01
  • 1
    Also, when you refer to a column, don't quote it (unless you're using `[[`). `"Position"` isn't the position column, it's just a string. The column is `df.clean$Position` or `df.clean[["Position"]]`, or inside `mutate()` and other `dplyr` functions just `Position` without quotes. – Gregor Thomas Mar 29 '23 at 17:04
  • 1
    If you convert it to numeric, then any of these should work: `df.clean$habitat <- ifelse(df.clean$Position >=2000, "interior", "edge")` or `df.clean <- df.clean %>% mutate(habitat = case_when(Position > 2000 ~ "interior", "Position" < 2000 ~ "edge"))` – Gregor Thomas Mar 29 '23 at 17:07
  • 1
    Thank you, the first one worked. And thank you for correcting me on my code in general. – Marnee Roundtree Mar 29 '23 at 17:23

0 Answers0