-1

For a biology master project I have a dataset will the number of dead animals per day per specie. Here is a similar dataset:

a <- c("Date", "Specie", "Number of dead animals")
b <- c("2020-01-01", "Dog", "3")
c <- c("2020-01-02", "Dog", "4")
d <- c("2020-01-03", "Dog", "5")
e <- c("2020-01-01", "Cat", "6")
f <- c("2020-01-02", "Cat", "3")
new_df <- data.frame(a, b, c, d, e, f)
View(new_df)

First, I want to get rid-off the a;b;c;d;e;f, that is the dataset should look like

Date / Specie / Number 
2020-01-01/Dog/3 
etc. 

Then, I want to create a new variable (mutate) that takes, for all species the number of dead animal at the date of 2020-01-02. That is, I want R to 1° select the value of dead animal the 2nd January for all species - 2° create a new column that has this value

I only could do it by creating a new dataset and subsetting at the date of 2020-01-02 but I'm sure sth much easier is possible. How ??

Thank's in advance!

For completeness, once I have this value, I must sort them by quantile. This code works but creates a new dataset. I want to perform this with the same dataset.

dataset_02 <- subset(new_df, date == "2020-01-02")
dataset_02 <- within(dataset_02, quantile <- as.integer(cut(Dead02, quantile(Dead02, probs=0:5/5), include.lowest=TRUE)))
dreamR
  • 25
  • 4
  • Please add what is the expected output. Also, for `subset(new_df, date == "2020-01-02")` , I get an error. You can take a look [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for how to reformulate your question. – Maël Feb 01 '22 at 11:00

1 Answers1

0

First, bind rows with rbind and add names with names. Then, group_by Specie and assign the value of Number of dead animals when Date == "2020-01-02".

df <- as.data.frame(rbind(b, c, d, e, f))
names(df) <- a

library(dplyr)
df %>% 
  group_by(Specie) %>% 
  mutate(Dead02 = `Number of dead animals`[Date == "2020-01-02"])

output

  Date       Specie `Number of dead animals` Dead02
  <chr>      <chr>  <chr>                    <chr> 
1 2020-01-01 Dog    3                        4     
2 2020-01-02 Dog    4                        4     
3 2020-01-03 Dog    5                        4     
4 2020-01-01 Cat    6                        3     
5 2020-01-02 Cat    3                        3     
Maël
  • 45,206
  • 3
  • 29
  • 67
  • The code doesn't work. I have the following message error. "Error: Problem with `mutate()` column `Dead02`. i `Dead02` must be size 9 or 1, not 0. i The error occurred in group 212: Specie = "Girafe". I think this is because I don't have for certain specie the number of dead animals for this date. – dreamR Feb 01 '22 at 10:14
  • That is, for some specie I have the number of dead animals for the date I am interested in. And for some others, I don't. – dreamR Feb 01 '22 at 10:16
  • The code works with the example you provided. You can use ```mutate(Dead02 = ifelse(any(Date == "2020-01-02"), `Number of dead animals`[Date == "2020-01-02"], NA))``` – Maël Feb 01 '22 at 10:18
  • Yes, I meant for my whole dataset sorry ;) Still, I try this and I get the following. Error: Problem with `mutate()` column `Dead02`. i `Dead02= ifelse(...)`. x comparison (1) is possible only for atomic and list types i The error occurred in group 1: Specie= Dog. – dreamR Feb 01 '22 at 10:27
  • And obvioulsy, no clue what to do... (please see my edit in main question, maybe that will help) – dreamR Feb 01 '22 at 10:27
  • Without a reproducible example, it's difficult to know. Please update your question with a minimal dataset that reproduces the error. – Maël Feb 01 '22 at 10:33