Skip to content

Instantly share code, notes, and snippets.

@opplatek
Created May 24, 2023 22:55
Show Gist options
  • Save opplatek/e4f80b4530f8c6d161d0d1fe7e77829a to your computer and use it in GitHub Desktop.
Save opplatek/e4f80b4530f8c6d161d0d1fe7e77829a to your computer and use it in GitHub Desktop.
#
# Convert apply() result to data.frame
#
# Sample data frame
df <- data.frame(A = c(1, 2, 3),
B = c(4, 5, 6))
# Sample function
calculate_sum <- function(row) {
sum(row)
}
# Apply function
result <- apply(df, 1, calculate_sum)
# Convert to data.frame
result_df <- do.call(rbind, lapply(result, as.data.frame))
print(result_df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment