简体   繁体   中英

Conditional change of a column in a data frame

Apologies in advance if this has already been asked elsewhere, but I've tried different attempts and nothing has worked so far.

I have a data frame Data containing the measurements of air pollution. The columns "Measuring.Unit" and "Uncertainty.Unit" show that most of the measurements are expressed in "mol/L" but some of them are expressed are "mol/mL.

head(Data)
     Locality.Name  Chemical  Concentration  Measuring.Unit Uncertainty Uncertainty.Unit 
1        xxxx       NH3       0.065          mol/L         0.010          mol/L  
2        xxxx       CO        0.015          mol/L         0.004          mol/L
3        xxxx       CO2       0.056          mol/L         0.006          mol/L
4        xxxx       O3        0.67           mol/mL        0.010          mol/mL
5        xxxx       H2SO4     0.007          mol/L         0.0008         mol/L
6        xxxx       NO        0.89           mol/mL        0.08           mol/mL                                       

Before starting any analysis, I want to change each value expressed in mol/mL in mol/L using a simple function and of course, change the associated character "mol/mL" in "mol/L". This should be something like this (but I guess there are much simple ways using dplyr or tidyverse ):

# First step
      if (Data$Measuring.Unit == "mol/mL")  {Data$Concentration <- Data$Concentration * 1000 } 
       else {Data$Concentration <- Data$Concentration } 

       if (Data$Uncertainty.Unit == "mol/mL")  {Data$Uncertainty <- Data$Uncertainty * 1000 } 
       else {Data$Uncertainty <- Data$Uncertainty} 

# Second step
Data$Measuring.Unit[Data$Measuring.Unit == 'mol/mL'] <- 'mol/L'
Data$Uncertainty.Unit[Data$Uncertainty.Unit == 'mol/mL'] <- 'mol/L'

You can try:

Data$Concentration <- ifelse(Data$Measuring.Unit == "mol/mL",Data$Concentration * 1000,Data$Concentration)

Data$Uncertainty <- ifelse(Data$Uncertainty.Unit == "mol/mL",Data$Uncertainty * 1000,Data$Uncertainty)

This step looks fine:

Data$Measuring.Unit[Data$Measuring.Unit == 'mol/mL'] <- 'mol/L'
Data$Uncertainty.Unit[Data$Uncertainty.Unit == 'mol/mL'] <- 'mol/L'

if() is used for values while ifelse() is vectorized for dataframes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM