0

so my data looks like this:

 data1  data2  data3 data4
 1      2      3     4
 2      3      4     5
 3      4      5     6

I want to take the mean of the first three columns of data for each row and get a product sort of like this:

 mean_123  data4
 2         4
 3         5
 4         6

I know I'll probably need an for loop. My data is coming from a data frame that has the 4 columns of data in it.

Let's call it "data_frame_1"

haramassive
  • 163
  • 1
  • 8
  • Use `apply(data_frame_1, 1, mean)` or `rowMeans(data_frame_1)`. Also see: https://stackoverflow.com/questions/30471302/how-to-calculate-row-means-in-a-data-frame – Rich Pauloo Nov 07 '18 at 01:58
  • Be careful, using `apply` on a frame that has non-`numeric` columns will convert everything to something non-numeric (e.g., `character`, which doesn't add well). If using `apply` is the way to go, always subset the columns of your frame to include only the needed columns. In this case it works without selecting columns, but keep it in mind with more complex datasets. – r2evans Nov 07 '18 at 02:00
  • 1
    To note, `rowMeans()` will run faster than the apply mean function option. – Evan Friedland Nov 07 '18 at 02:07
  • 2
    Possible duplicate of [Calculate row means on subset of columns](https://stackoverflow.com/questions/10945703/calculate-row-means-on-subset-of-columns) – dww Nov 07 '18 at 02:42

2 Answers2

0

Here's a dplyr solution which uses rowwise() in order to calculate the row means. I used transmute() here because it seemed like you wanted to drop the columns used to generate the means. If you want to keep them, use mutate() instead. I hope this helps - good luck!

Code:

library("dplyr")

mean_df <- rowwise(df) %>%
  transmute(mean_123 = mean(c(data1,data2,data3)),
            data4 = data4)

Output:

# A tibble: 3 x 2
  mean_123 data4
     <dbl> <dbl>
1        2     4
2        3     5
3        4     6
Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
0

With base R, just use data.frame(mean_123 = rowMeans(data_frame_1[1:3]), data4 = data_frame_1$data4)

#  mean_123 data4
#1        2     4
#2        3     5
#3        4     6
nghauran
  • 6,648
  • 2
  • 20
  • 29