11

How to calculate the average value of two lists for each row

 2 years ago
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.
neoserver,ios ssh client

How to calculate the average value of two lists for each row

advertisements

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?

I have the following table: [1,] 434 359 [2,] 8012 8217 [3,] 1254 1360 [4,] 39 112 [5,] 4322 4199 [6,] 595 2737 [7,] 12984 13112 [8,] 5597 4287 I want to plot a histogram of the smallest value from each row. I know the hist() function in R, but I don

How to get the corresponding values ​​in two lists

So I have two lists called A and C that each have the same amount of data points stored in them. I have created a new list called E that contains values in C that are greater than 400. I need to make a plot of E vs. A My question is how do I get the

How to calculate the average values ​​in the list by omitting a special value?

How to calculate mean of values in the list omitting a special value (-999)? import numpy as np A = [4,5,7,8,-999] M = np.mean(A) Any idea ??? >>> import numpy as np >>> a = np.array([1,2,3,4,5]) >>> np.mean(a) 3.0 >>>

How to calculate the average values ​​of the large datasets

I am working with a dataset that has temperature readings once an hour, 24 hrs a day for 100+ years. I want to get an average temperature for each day to reduce the size of my dataset. The headings look like this: YR MO DA HR MN TEMP 1943 6 19 10 0 7

How to calculate the percentage value between two points

This is probably a really dumb question but.. I have 3 values for example lets call them minX, currentX and maxX. im looking for a way to calculate the percentage of maxX that currentX represents. e.g if maxX is 50 and minX is 40 and currentX is 45 t

How to find the average of a continuous variable for each categorical variable

I'd like to plot continuous BMI on the y-axis and the categorical variable for Family Income on the x-axis and I'd like the graph to plot the mean BMI for each category. However, I am not sure how to find the mean BMI for each factor of Family Income

How to save the edittext value in a list for android sqlite?

Quiz activity how can i get all the value of the edit text in the list view and save it to sqlite DB? i already did some research on google for this but no luck, i really don't have any idea, can anyone give me a sample, any help will be very much ap

How to select the nth char of a string for each row in a file?

Every line has a word and a number. I need somehow to select the nth letter which all together will make a new word. For example: and 3 for 3 map 2 wrestle 1 draw it has to start like this cat char.txt | ... and I'm only allowed to use sed (no awk, p

The count value only grabs 1 for each row

I am making a stats page about golf for the people I play with. I am trying to pull out of the database the number of times out of all our scorecards that we received birdies (which is -1 under par). It does pull out the -1s per hole, however I notic

Pandas: Get the highest value of a column for each unique value in another column

How to get the highest value in one column for each unique value in another column and return the same dataframe structure back. Here is a pandas dataframe example? reg.nr counter value ID2 categ date 1 37367 421 231385 93 A 20.01.2004 2 37368 428 23

Calculate and return the average values ​​in a list

I have a long list with some values. I want to define a function that take the list and calculates the average for every 24 values in the list, and returns the average values as a list. How do I do this? I have 8760 elements in the list, and the list

How to calculate the average range of a Quad?

Not sure if I formulate the correct answer but basically I need to know how to calculate the average size of a stretch quad, or something like taking the 2 diagonals AC and BD how can I calculate the average of them The blue square show its original

How to calculate the average of the last 100 numbers of increasing number of groups

I have a data source. It gives a number each minute. I want to compute the average value of the last 100 numbers. How do I do this with C? For example, the input is 1,2,3,4, ... 150 then I want to get the average value of 50 . . . 150. One minute lat

How to calculate the average point of each student on each course by mysql

I will use the above example to make you clear. student_id | c1 | c2 | c3 | c4 1 | 85 | 90 | 73 | 99 2 | 56 | 85 | 86 | 96 3 | 23 | 74 | 54 | 88 My Question is how to calculate the average score of all the students at the same time. The result would

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK