Roland Roland - 11 months ago 52
R Question

Excel if statement to R codes

I am new to R and trying to use R to run the report I am currently doing in excel. Most of the topics here have been so helpful to me translating excel formula to R codes, however, I am struggling to generate codes for below excel if statement


  • G Column corresponds to Container/Product

  • F Column corresponds to Transport Mode

  • AI and AJ correspond to the volumes associated to each Transport mode

Appreciate all the help. Thanks

Here's the link to data exported to R

Answer Source

We can do a nested ifelse after reading the dataset

df1 <- read.csv("yourfile.csv", stringsAsFactors=FALSE)
ifelse(df1[,7]=="SEA" & df1[,6] %in% c("FCL", "BCN"),
        df1[,35]*40, ifelse(df1[,7]=="AIR", df1[,36]*66, NA))

NOTE: Here we are referring to numeric index for extracting the columns as a reproducible example was not showed.