-2

I have a data set on football transfer fees from across various leagues, how can I filter the data to only give me the data from the following leagues Premier League, La Liga, Ligue 1, Serie A and Bundesliga. I then need to find the mean transfer fee for each league and plot this. I have attached an image of the dataset I need to filter.

FT_League1<-FootballTransfers[c(2:742),c(1,7,8,10)] 

I tried this but I still need to subset column 7 to only give me transfer fees from the leagues I want

I have attached an image of the output this gave me

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66
Cian
  • 5
  • 1
  • 2
    Hey, welcome to SO! Please provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). This makes communication much easier. Posting your data as a picture is not helpful, because we can not transfer data from it to our own R session. – uke Apr 08 '23 at 23:57

1 Answers1

0
FT_League1[FT_League1$name=="Premier League"|FT_League1$name=="La Liga"|FT_League1$name=="Ligue 1"|FT_League1$name=="Serie A"|FT_League1$name==" Bundesliga",]

It seems you understand how R subsets via [row,column] so all this is doing after the operation in your post is then filtering only the leagues with the names you specified in your question. the | operator stands for OR. So, in English this code says something akin to "from the dataframe "FT_League1", I would like to keep all rows where the variable name is equal to "Premier League" OR name is equal to "La Liga"...and so an and so forth. Then you could use aggregate() to get the means of each league.

If you prefer dplyr:

FT_League1 %>% 
filter(name==c("Premier League","La Liga","Ligue 1", "Serie A","Bundesliga") %>%
group_by(name) %>%
summarise(meanfee=mean(Transfer_fee)) %>%
ungroup()

Note this code is untested as you did not provide reproducible data to work with. Hope it helps.

dandrews
  • 967
  • 5
  • 18