Joel Harding
Joel Harding

Reputation: 23

Condtional statement to rbind if object exists

I want to rbind and object called old_dat to main data object called dat only if it exists, otherwise dat remains unchanged.

old_dat is created using a separate function.

I have tried the following two methods:

 dat<- ifelse(exists("old_dat", envir = .GlobalEnv),
              rbind(dat,old_dat),dat)

and

exists("old_dat") && dat<- rbind(dat,old_dat)

Upvotes: 2

Views: 3828

Answers (1)

Mark O&#39;Connell
Mark O&#39;Connell

Reputation: 295

dat <- rbind(dat, if(exists("old_dat")) old_dat)

Upvotes: 14

Related Questions