begining in R
begining in R

Reputation: 11

Sample R code to extract specific columns

My data looks like this:

      FlightID FareClass FareClassRank FareValue Bookings
1 YULCDG215135         Q             1       100        5
2 YULCDG215135         X             2       150        7
3 YULCDG215135         V             3       200        4
4 YULCDG215135         Y             4      1000        2
5 YULCDG215136         Q             1       120        1
6 YULCDG215136         X             2       200        4
7 YULCDG215136         V             3       270        5
8 YULCDG215136         Y             4       900       15

Question: I need to write simple code in R:

For each flight, the ratio of the value of the fare class to the value of its next fare class.

As example, X is the next fare class of Q, V is the next fare class of X, and so on.

The ratio for flight YULCDG215135 is 100/150 = 0.6667 for X, 150/200=0.75 for V and so on.

Upvotes: 0

Views: 75

Answers (3)

Santiago Capobianco
Santiago Capobianco

Reputation: 886

Try this:

df$FareOverNext <- unlist(lappy(split(df$FareValue, df$FlightID), {
 c(1, x[1:(length(x) - 1)] / x[2:length(x)])
}))

EDIT:

Added lapply and split thanks to @thelatemail comment.

Upvotes: 0

iod
iod

Reputation: 7592

require(dplyr) 
df %>% 
group_by(FlightID) %>% 
arrange(FareClassRank) %>%
mutate(ratio=FareValue/lead(FareValue)) 

Upvotes: 2

Maurits Evers
Maurits Evers

Reputation: 50668

Here is a base R solution using by.

by(df, df$FlightID, function(x) c(NA, exp(-diff(log(x$FareValue)))))
#df$FlightID: YULCDG215135
#[1]        NA 0.6666667 0.7500000 0.2000000
#------------------------------------------------------------
#df$FlightID: YULCDG215136
#[1]        NA 0.6000000 0.7407407 0.3000000

Or alternatively using ave

transform(df, ratio = ave(FareValue, FlightID, FUN = function(x) c(NA, exp(-diff(log(x))))))
#      FlightID FareClass FareClassRank FareValue Bookings     ratio
#1 YULCDG215135         Q             1       100        5        NA
#2 YULCDG215135         X             2       150        7 0.6666667
#3 YULCDG215135         V             3       200        4 0.7500000
#4 YULCDG215135         Y             4      1000        2 0.2000000
#5 YULCDG215136         Q             1       120        1        NA
#6 YULCDG215136         X             2       200        4 0.6000000
#7 YULCDG215136         V             3       270        5 0.7407407
#8 YULCDG215136         Y             4       900       15 0.3000000

The trick in both cases is to log-transform FareValue so that we can use diff, and then to invert the transformation (using exp) to give the ratio.

Upvotes: 3

Related Questions