Home > Back-end >  Using if or ifelse
Using if or ifelse

Time:10-06

I have a dataframe in which I need to make a single observation in a column different based on the value of a variable in another column. I am new to programming and am having a difficult time finding a concrete example that I can follow. When the Item value is 1615, Epoch should be 261, and at all other levels of Item, Epoch should not change. The dataframe name is myevents. Here is an example of the data:

Item Epoch
1612 260
1613 0
1614 0
1615 0
1616 0
1617 0
1618 262
1619 0

Here is what the output should look like:

Item Epoch
1612 260
1613 0
1614 0
1615 261
1616 0
1617 0
1618 262
1619 0

Here is what I have tried:

myevents$Epoch=ifelse(myevents$Item==1615, myevents$Epoch==261, myevents$Epoch==myevents$Epoch)

myevents$Epoch=ifelse(myevents$Item==1615, myevents$Epoch==261, myevents$Epoch)

myevents$Epoch <- with(myevents, ifelse(myevents$Item==1615, myevents2$Epoch==261, myevents$Epoch==myevents$Epoch))

myevents$Epoch <- with(myevents, if(myevents$Item==1615, myevents$Epoch==261))

None of the above worked, and some actually replace the column with all TRUE or FALSE characters instead. I have made sure all numbers are being treated as numeric, and for good measure I did try wrapping them in single quotes just in case. I've also tried using forms if just if alone but I cannot figure out how to use it in a way that actually runs. Any help is greatly appreciated!

CodePudding user response:

Your code is almost working. the main confusion seems to come from the usage of ifelse (which you had the right intuition to use):

myevents$corrected <- ifelse(myevents$Item==1615, 261, myevents$Epoch)

Or, if you want to overwrite directly:

myevents$Epoch <- ifelse(myevents$Item==1615, 261, myevents$Epoch)

ifelse returns a value, that we assign to a variable, we don't assign in the ifelse statement itself. (or more precisely, ifelse returns either its second or third argument, depending on the outcome of the comparison in the first argument)

CodePudding user response:

Another way to do without if or ifelse would be this if I understood well your question:

myevents[myevents$Item %in% 1615,"Epoch"]= 261

CodePudding user response:

You already have several answers with the code for base R, so I left it out of my answer. However, when you called each vector independently (data$column) you were no longer linking the different rows. You can certainly use some of the great base R answers you already have, but here are a couple of other options using tidyverse.

library(tidyverse)

# random data to work with for the post
set.seed(3625)                 # set seed for the sample()
df <- data.frame(Item = sample(rep(1610:1620, times = 5)))
#    Item
# 1  1612
# 2  1611
# 3  1616
# 4  1616
# 5  1612
# 6  1620
# 7  1618
# 8  1619
# 9  1617
# 10 1615
#... 55 rows
    
df <- df %>%           # using ifelse
  mutate(Epoch = ifelse(test = Item == 1612,
                        yes = 260,
                        no = ifelse(test = Item == 1615,
                                    yes = 261,
                                    no = ifelse(test = Item == 1618,
                                                yes = 262,
                                                no = 0))))
#    Item Epoch
# 1  1612   260
# 2  1611     0
# 3  1616     0
# 4  1616     0
# 5  1612   260
# 6  1620     0
# 7  1618   262
# 8  1619     0
# 9  1617     0
# 10 1615   261 
#... 55 rows

df <- df %>% 
  mutate(Epoch2 = case_when(      # alternative to ifelse
    Item == 1612 ~ 260,
    Item == 1615 ~ 261,
    Item == 1618 ~ 262,
    TRUE ~ 0))
#    Item Epoch Epoch2
# 1  1612   260    260
# 2  1611     0      0
# 3  1616     0      0
# 4  1616     0      0
# 5  1612   260    260
# 6  1620     0      0
# 7  1618   262    262
# 8  1619     0      0
# 9  1617     0      0
# 10 1615   261    261 
#... 55 rows
  • Related