Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active November 9, 2022 12:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save primaryobjects/33adabc337edd67b4a8d to your computer and use it in GitHub Desktop.
Save primaryobjects/33adabc337edd67b4a8d to your computer and use it in GitHub Desktop.
2 Ways to Return Multiple Values with sapply in R
2 Ways to Return Multiple Values with sapply
---
Demo at http://www.r-fiddle.org/#/fiddle?id=4pR9ndzC&version=2
Assume we have the following data:
Date Value
1 2015-05-01 73
2 2015-05-01 94
3 2015-05-02 100
4 2015-05-02 83
5 2015-05-03 47
6 2015-05-04 24
We want to convert this data by day, into the following format, for easy display in a chart:
Count Total Date
2 167 2015-05-01
2 183 2015-05-02
1 47 2015-05-03
2 100 2015-05-04
2 123 2015-05-05
1 32 2015-05-06
We can use sapply over the data.frame to convert the data. During each loop iteration in saaply, we can either populate an outside data.frame with a new row or we can return a vector of the result values in each iteration. See Method 1 and Method 2 in the code example below.
With our data formatted, we can plot a chart as shown in the example below.
# Create some sample data.
dates <- seq(as.Date("2015/5/1"), as.Date("2015/6/30"), length.out=100)
values <- sample(1:100, length(dates))
# Create a data.frame of data.
data <- data.frame(Date = dates, Value = values)
# Split by day.
dataByDay <- split(data, data$Date)
# Get list of data counts and sum per day.
# Method 1.
dataCountsByDay1 <- data.frame()
x <- sapply(dataByDay, function(r) {
df <- data.frame(r)
row <- data.frame(Count = nrow(df), Total = sum(df$Value), Date = df[1,]$Date)
dataCountsByDay1 <<- rbind(dataCountsByDay1, row)
})
# Get list of data counts and sum per day.
# Method 2.
dataCountsByDay2 <- as.data.frame(t(sapply(dataByDay, function(r) {
df <- data.frame(r)
c(Count = nrow(df), Total = sum(df$Value))
})))
dataCountsByDay2$Date <- as.Date(row.names(dataCountsByDay2))
# Verify they're both equivalent.
dataCountsByDay1$Count == dataCountsByDay2$Count
dataCountsByDay1$Total == dataCountsByDay2$Total
as.character(dataCountsByDay1$Date) == as.character(dataCountsByDay2$Date)
nrow(dataCountsByDay1) == nrow(dataCountsByDay2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment