mercredi 19 août 2020

Creating a new variable in a dataset from data withing the same dataset using ifelse statements

I am new to R and I am trying to sort some data for a project. Basically I have been given a dataset with four variables and I want to combine two of them to make a fifth variable. The variables I am trying to combine are Day of the week and AM/PM to make a 'time of week' variable (e.g Mon AM, Mon PM etc). I am using RStudio to compile the data.

project.dat contains four variables:

Company | Day | Time(AM/PM) | Price

I want to create a fifth variable for petrol that is a combination of Day and Time:

Company | Day | Time(AM/PM) | Price | TOW

I have tried two approaches to this:

1: Using ifelse to check for the time of day, then using a placeholder to add in the day. I have used a variety of placeholder operators I have found on the net, this just happens to be the last one I tried.

attach(project.dat)
Timeweek <- ifelse(Time=="am", "#% AM" ["Day"], "#% PM", ["Day"])

petrol <- project.dat

petrol$TOW <- Timeweek

detach(petrol)

This method worked in filtering the time of day but I could not get the placeholder to add the Day into the new variable.

2: Checking for day and time of day individually and adding the new variable individually. This method went through and assigned 'Mon AM' to every line. If I remove the first line, it assigns 'Undefined' to every line. I have used this approach in other parts of the project without any trouble, but the other applications had only a single condition (i.e no &&).

attach(project.dat)
 
Timeweek <- ifelse(Day=="Mon" && Time=="am", "Mon AM",
            ifelse(Day=="Mon" && Time=="pm", "Mon PM",
            ifelse(Day=="Tue" && Time=="am", "Tue AM",
            ifelse(Day=="Tue" && Time=="pm", "Tue PM",
            ifelse(Day=="Wed" && Time=="am", "Wed AM",
            ifelse(Day=="Wed" && Time=="pm", "Wed PM",
            ifelse(Day=="Thur" && Time=="am", "Thur AM",
            ifelse(Day=="Thur" && Time=="pm", "Thur PM",
            ifelse(Day=="Fri" && Time=="am", "Fri AM",
            ifelse(Day=="Fri" && Time=="pm", "Fri PM",
            ifelse(Day=="Sat" && Time=="am", "Sat AM",
            ifelse(Day=="Sat" && Time=="pm", "Sat PM",
            ifelse(Day=="Sun" && Time=="am", "Sun AM",
            ifelse(Day=="Sun" && Time=="pm", "Sun PM",
            "Undefined")))))))))))))      

petrol <- project.dat

petrol$TOW <- Timeweek

detach(petrol)

First few lines (sorted by company) of original data set (in stars is the data I am trying to add):

   Company   Day   Time   Price   **TOW**
1   BP        Mon   am     102.3   **Mon AM**
2   BP        Mon   am     108.1   **Mon AM**
3   BP        Mon   am     112.0   **Mon AM**
4   BP        Mon   pm     110.2   **Mon PM**
5   BP        Mon   pm     115.1   **Mon PM**
16  BP        Tue   am     102.5   **Tue AM**
17  BP        Tue   am     112.1   **Tue AM**
18  BP        Tue   am     118.7   **Tue AM**

Aucun commentaire:

Enregistrer un commentaire