Roland Roland - 2 months ago 8
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

=IF(AND(G2="SEA",OR(F2="FCL",F2="BCN")),W2*40,IF(G2="AIR",X2/1000*66,""))



  • 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

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.

Comments