Sort one column based on another column

I'm a new rstudio user, and hoping for some guidance. I have imported data into r, and am trying to sort one column (crop yield) based on a 2nd column (precipitation). Specifically, I'm trying to take the average crop yield of the 5 wettest years.

My current code is as follows:

Data = read.table("crop.output.dat-ARL-0N-SCEN1")
colnames(Data) <- c("Year","PFT","Yield (bu/ac)","Fertilizer+Manure N (kg/ha)","N-Minearalization(kg/ha)","	Crop N uptake (kg/ha)","NO3-N leaching (kg/ha)",	"NO3-N leaching (ppm)",	"Harvest Index",	"Leaf N concentration",	"Stem N concentration",	"Root N concentration",	"Grain N concentration",	"Grain N (kg/ha)",	"Drainage (mm/yr)","Precip (mm/yr)",	"Irrigation(mm/yr)",	"LAI max",	"N fixation (kg/ha)",	"SOC kg/m2",	"Planting DOY",	"Harvest DOY")
  with (Data, order(Data$`Precip (mm/yr)`)),

Hi Tracy, welcome!

You can sort a dataframe by any column or combination of columns with the arrange() function from dplyr, see this example:


iris %>%
    arrange(Sepal.Width) %>% 
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 1          5.0         2.0          3.5         1.0 versicolor
#> 2          6.0         2.2          4.0         1.0 versicolor
#> 3          6.2         2.2          4.5         1.5 versicolor
#> 4          6.0         2.2          5.0         1.5  virginica
#> 5          4.5         2.3          1.3         0.3     setosa
#> 6          5.5         2.3          4.0         1.3 versicolor

If you need more specific help, please provide a minimal REPRoducible EXample (reprex) illustrating your issue. A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

1 Like

You can try this:

with(Data, mean(`Yield (bu/ac)`[order(-`Precip (mm/yr)`)]))
1 Like