Home > other >  Filling a column with conditions and formulas
Filling a column with conditions and formulas

Time:11-04

I am working on calculating the area of flowering parts in certain plots. However, different types of plants have different types of measurements (for example, some flowers, we have the size of the whole inflorescence and some we have only the size of individual flowers). I would like to create a column with the calculated area using different conditional statements. Here is an example data frame:


inflorescence_mm <- c("5", "NA", "NA")
flower_mm <- c("NA", "NA", "3")
corolla_mm <- c("NA", "2", "NA")
count <- c("100", "75", "80")

df <- data.frame(inflorescence_mm, flower_mm, corolla_mm, count)

I would like to create a column called "flower_area_mm2" using mutate and ifelse, but since I am using a formula to calculate area, I am having trouble.

If there is data in inflorescence_mm, then I would use (0.5inflorescence_mm)^2 * pi * count. If there is an NA in inflorescence, then I would use (0.5flower_mm)^2 * pi * count. And if there is an NA in flower_mm then I would use (0.5*corolla_mm)^2 * pi * count.

Can anyone help write such a conditional statement?

I tried creating an ifelse statement within mutate and using is.na, but this did not fill in the new column.

CodePudding user response:

Perhaps your problem is due to the format of your df columns. In this case, if you convert the columns to integer, you can implement the mutate and ifelse function.

df %>% 
  mutate_if(is.character, as.integer) %>% 
  mutate(flower_area_mm2 = ifelse(!is.na(inflorescence_mm), 
                                  (0.5*inflorescence_mm)^2 * pi * count, 
                                  (0.5*corolla_mm)^2 * pi * count))

OUTPUT:

  inflorescence_mm flower_mm corolla_mm count flower_area_mm2
1                5        NA         NA   100       1963.4954
2               NA        NA          2    75        235.6194
3               NA         3         NA    80              NA

CodePudding user response:

I ended up solving it by using case_when:

mutate(flower_area_mm2 = case_when(!is.na(inflorescence_mm) ~ 
                                   (0.5*inflorescence_mm)^2 * pi*count,
                                 is.na(flower_mm) ~ (0.5*corolla_mm)^2 * pi * count,
                                 is.na(inflorescence_mm) ~ (0.5* flower_mm)^2 * pi*count))
  • Related