I'm trying to use sapply to take each item in a list (e.g. "Golf","Malibu","Corvette") and create a new list with the highest value in the dataframe that list was split from (e.g. cars$sale_price). I'm trying to use an anonymous function to do so, but I can't get that function to work.
The basic issue here is that I'm not very good at writing functions.
First, I took the original dataframe cars and used split
to create a list of unique car names - I called this car_names.
Now, I'm trying to create a new list, using sapply, of the highest sale price of each type of car in the list. I'm sure I'm starting the thing correctly ...
price_list <- sapply(car_names,
... but I can't for the life of me get an anonymous function to simply apply max
to all instances of each car name in cars$sale price.
I've tried a bunch of stuff, all of which has returned an error. Here's an example:
price_list <- sapply(car_names, function(x) {
max(cars$saleprice[x])
})
Which returns:
Error in h115$nominate_dim1[x] : invalid subscript type 'list'
I'm sure this is trivially simply for even moderate experienced programmers, but I'm ... not one of those! I suspect that I'm pointing to something incorrectly, but I can't get past it. Any ideas?
Edit: Here's a reproducible example.
First, the "source" dataframe:
cars1 <- data.frame("car_names" = c("Corvette", "Corvette", "Corvette", "Golf", "Golf", "Golf", "Malibu", "Malibu", "Malibu"),"saleprice" = c(32000,45000,72000,7500,16000,22000,33000,21000,26500))
Next, splitting the df by car_names:
cars1_split <- split(cars1, cars1$car_names)
Now, attempting to pass max
to sapply
and getting an error:
maxes <- sapply(cars1_split, function(x){
max(cars1$saleprice[x])
})
Hopefully this give you guys something to work with!