

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
-
38
Array iteration methods are like a ‘gateway drug’.They get many people hooked on functional programming. Because they’re just so darn usefu...
-
14
Photo by Johannes Plenio on UnsplashUnderstanding wha...
-
5
How to calculate the average score for each student advertisements I have a table "Register" with columns: ...
-
6
Calculate each matrix element advertisements Here is what I have the formula I have to compute for each element of my Numpy matrices :
-
9
lany yesterday at 13:33One does not simply calculate the absolute value Translation
-
13
How to Calculate the Value of nCr By Yuvraj Chandra Published 20 hours ago Learn how to calculate a Combination in five program...
-
5
How does the dialog manager calculate the average width of a character?
-
12
Here's the average salary each generation says they need to feel 'financially healthy.' Gen Z requires a whopping $171K/year — but how do your own expectations compare?
-
7
How to Find Average Marks of Each Subject in SQL? ...
-
7
How to Find Average Marks of Each Student in SQL? ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK