Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Presence/Absence column in a data frame [duplicate]

Tags:

r

tidyverse

I have a data frame that looks more or less like this

df <- tribble(
  ~Species, ~Symbiont, ~Location,
  "a",   "s01", "P01",
  "b",   "s02", "P01",
  "c",   "s01", "P02",
  "a",   "s03", "P03",
  "d",   "s02", "P02",
  "c",   "s04", "P04",
  "a",   "s03", "P01",
  "c",   "s02", "P02",
  "c",   "s03", "P02"
)

What I'd like to to achieve is to have a single row for each Species in each Location, and a presence-absence 0-1 info about the Symbiont.

For example, with the given data,


Species s01  s02  s03  s04 Location
   a     1    0    1    0    P01
   a     0    0    1    0    P03
   b     0    1    0    0    P02
   c     1    1    1    0    P02
   c     0    0    0    1    P04
   d     0    1    0    0    P02

I'd like to use tidyverse as I'm more comfortable with it but whatever works

I'll spare you the goofy attempt I've tried...

Thank you!

like image 443
arteteco Avatar asked Oct 18 '25 18:10

arteteco


1 Answers

First, add a presence/absence indicator to your tibble. As you only have "presence"s to start with, every value will be 1

df %>% add_column(Present=1)

Now use pivot_wider to untidy your data

df %>% 
  add_column(Present=1) %>% 
  pivot_wider(names_from=Symbiont, values_from=Present)

Finally, replace the NAs we've introduced, which correspond to absences, with 0s.

df %>% 
  add_column(Present=1) %>% 
  pivot_wider(names_from=Symbiont, values_from=Present) %>% 
  replace(is.na(.), 0)

Giving

# A tibble: 6 x 6
  Species Location   s01   s02   s03   s04
  <chr>   <chr>    <dbl> <dbl> <dbl> <dbl>
1 a       P01          1     0     1     0
2 b       P01          0     1     0     0
3 c       P02          1     1     1     0
4 a       P03          0     0     1     0
5 d       P02          0     1     0     0
6 c       P04          0     0     0     1
like image 144
Limey Avatar answered Oct 21 '25 09:10

Limey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!