简体   繁体   中英

Write a FUNCTION in R

I am a beginner and find it very difficult to write a function in R. I have a dataset with 61 rows and 20 columns. This is the preview of the dataset

I want to write a function which calculates the difference between the two columns, "GoalsFor" and "GoalsAgainst" and Using the same function, I have to find the team that which has maximum and minimum goal difference.

I have managed the first part but I am not getting how to do the second part using same function.

Here is a simplified version of your table written in code. In the future do something like this in your question instead of a picture. It makes it so that others have to do less work when helping you.

teamStats = data.frame(
  Pos = c(1,2,3,4,5),
  Team = c("Real Madrid", "Barcelona", "Atletico Madrid", "Valencia", "Athletic Bilbao"),
  GoalsFor = c(5947, 5900, 4534, 4398, 4631),
  GoalsAgainst = c(3140, 3114, 3309, 3469, 3700)
)

You say you were able to create the goal differences. As you say you're new in R, here's some sample code imagining what you as a beginner could have come up with. In the future, present something like this in your question as well. It helps us understand what you know and where you're stuck.

getGoalDifferences = function(ts) {
  Teams = c() 
  spreads = c()
  for(row in 1:NROW(ts)) {
    Teams = rbind(Teams, ts[row,"Team"])
    spreads = rbind(spreads, ts[row,"GoalsFor"] - ts[row,"GoalsAgainst"])
  }
  data.frame(Teams, spreads)
}

# usage
getGoalDifferences(teamStats)

Here is a working function you can use. It uses vector subtraction, appending to a newly created property. This avoids the need to create a new dataframe and to do looping. It then marks the minimum and maximum spreads, respectively. Finally, it outputs the marked rows.

findSpreadExtremes = function (ts) {
  
  ts$spread = ts$GoalsFor - ts$GoalsAgainst
  ts$marker[ts$spread == min(ts$spread)] = "min score spread"
  ts$marker[ts$spread == max(ts$spread)] = "max score spread"
  
  # Alternative Output Options, depending on what you want
  # ts # if you want the entire table with 'marker' column included
  # ts[!is.na(ts$marker),"Team"] # if you just want the team names
  # ts[!is.na(ts$marker),] # if you want the entire rows of extreme teams

  # if you want only certain columns
  ts[
    !is.na(ts$marker), # row filter
    c("Team","GoalsFor","GoalsAgainst","spread","marker") # column filter
  ] 
  
}

# usage
findSpreadExtremes(teamStats)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM