How to calculate the average value of two lists for each row
source link: https://www.codesd.com/item/how-to-calculate-the-average-value-of-two-lists-for-each-row.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to calculate the average value of two lists for each row
I have two lists as below:
l1 <- list(a=1, b=2, c=3)
l2 <- list(a=4, b=5, c=6)
I want to calculate the mean value of the two lists for each row and assign the result to a new list. Like below:
l3 <- mean(l1, l2)
How can I achieve this in R?
We can use Map
Map(function(x,y) mean(c(x,y)), l1, l2)
Or with map2
from purrr
purrr::map2(l1, l2, ~ mean(c(.x, .y)))
Or using tidyverse
library(tidyverse)
bind_rows(l1, l2) %>%
summarise_all(mean) %>%
unlist() %>%
relist(., skeleton = l1)
Or another option is
relist((unlist(l1) + unlist(l2))/2, skeleton= l1)
NOTE: All the solutions return a list
as the OP mentioned in the post
Related Articles
How to draw the minimum value between 2 columns for each row in a matrix?
How to get the corresponding values in two lists
How to calculate the average values in the list by omitting a special value?
How to calculate the average values of the large datasets
How to calculate the percentage value between two points
How to find the average of a continuous variable for each categorical variable
How to save the edittext value in a list for android sqlite?
How to select the nth char of a string for each row in a file?
The count value only grabs 1 for each row
Pandas: Get the highest value of a column for each unique value in another column
Calculate and return the average values in a list
How to calculate the average range of a Quad?
How to calculate the average of the last 100 numbers of increasing number of groups
How to calculate the average point of each student on each course by mysql
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK