Nested if function: the condition has length > 1 and only the first element will be used

Hi all,

My first post in the community, but I have been an avid reader of other posts :slight_smile: I am a beginner in R, and I am trying to build

I have a data set that contains a potential commercialization year and the sales forecast from Year 1 to 5 for each project. For different projects, year 1 might be 2023, 2024, 2028 and so on. I want to arrange the data in columns by year, so I can have it ordered.

I thought about doing it with an nested IF function, depending of the commercialization year. So far so good, but I get the following error when running the function:

the condition has length > 1 and only the first element will be used

Any ideas of what I might be doing wrong? Also, I accept any suggestions on how to do it. Here is what I have so far in my code.

Collabs_DB.df <- read_xlsx(path="Knime_output.xlsx")

Collabs_DB.df$newcolumn <- 0
Collabs_DB.df$newcolumn1 <- 0
Collabs_DB.df$newcolumn2 <- 0
Collabs_DB.df$newcolumn3 <- 0
Collabs_DB.df$newcolumn4 <- 0
Collabs_DB.df$newcolumn5 <- 0
Collabs_DB.df$newcolumn6 <- 0
Collabs_DB.df$newcolumn7 <- 0
Collabs_DB.df$newcolumn8 <- 0
Collabs_DB.df$newcolumn9 <- 0
Collabs_DB.df$newcolumn10 <- 0
Collabs_DB.df$newcolumn11 <- 0
Collabs_DB.df$newcolumn12 <- 0
Collabs_DB.df$newcolumn13 <- 0

Collabs_DB.df$Close_Year <- as.integer(Collabs_DB.df$Close_Year)

Collabs_DB.df <- plyr::rename(Collabs_DB.df, c(
"newcolumn" = "Y_2017",
"newcolumn1" = "Y_2018",
"newcolumn2" = "Y_2019",
"newcolumn3" = "Y_2020",
"newcolumn4" = "Y_2021",
"newcolumn5" = "Y_2022",
"newcolumn6" = "Y_2023",
"newcolumn7" = "Y_2024",
"newcolumn8" = "Y_2025",
"newcolumn9" = "Y_2026",
"newcolumn10" = "Y_2027",
"newcolumn11" = "Y_2028",
"newcolumn12" = "Y_2029",
"newcolumn13" = "Y_2030"


Collabs_DB.df <- mutate(Collabs_DB.df, Collabs_DB.df$Y_2020 = Collabs_DB.df$Close_Year)

if (Collabs_DB.df$Close_Year == 2019) {
Collabs_DB.df$Y_2020 = Collabs_DB.df$Sales Y1
Collabs_DB.df$Y_2021 = Collabs_DB.df$Sales Y2
Collabs_DB.df$Y_2022 = Collabs_DB.df$Sales Y3
Collabs_DB.df$Y_2023 = Collabs_DB.df$Sales Y4
Collabs_DB.df$Y_2024 = Collabs_DB.df$Sales Y5

Thanks in advance for the help!!!


Hi, welcome!

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.