Reputation: 145
So what I am trying to do is to create a mean for each row, but only including every second column. An example df would be:
C1<-c(3,2,4,4,5)
C2<-c(3,7,3,4,5)
C3<-c(5,4,3,6,3)
C4<-c(5,4,3,6,3)
C5<-c(5,6,3,6,2)
C6<-c(5,5,3,6,4)
C7<-c(5,6,3,6,1)
C8<-c(5,7,3,6,2)
DF<-data.frame(ID=c("A","B","C","D","E"),C1=C1,C2=C2,C3=C3, C4=C4, C5=C5, C6=C6, C7=C7, C8=C8)
DF
ID C1 C2 C3 C4 C5 C6 C7 C8
1 A 3 3 5 5 5 5 5 5
2 B 2 7 4 4 6 5 6 7
3 C 4 3 3 3 3 3 3 3
4 D 4 4 6 6 6 6 6 6
5 E 5 5 3 3 2 4 1 2
So now I would like to calculate the mean of C2, C4, C6 and C8 and add this as a new column in the df. What I tried is this:
DF$mean <- rowMeans(DF[seq(2, nrow(dat_all_b), 2),])
This is the error produced: "Fehler in $<-.data.frame
(*tmp*
, frequency_mean, value = c(350
= NA, :
replacement has 14 rows, data has 30"
Fehler = error
My actual df has way more columns and i want to calculate the mean across every second column. Expected output:
DF
ID C1 C2 C3 C4 C5 C6 C7 C8 mean_1
1 A 3 3 5 5 5 5 5 5 4,5
2 B 2 7 4 4 6 5 6 7 5,75
3 C 4 3 3 3 3 3 3 3 3
4 D 4 4 6 6 6 6 6 6 5,5
5 E 5 5 3 3 2 4 1 2 3,5
Any help will be appreciated!
Upvotes: 0
Views: 327
Reputation: 81
I would suggest to look into the apply family. In this case, by using apply and set the margin to 1 (by row -- while setting it to 2 will give you the mean by column) will solve the issue.
DF$mean <- apply(DF[,2:ncol(DF)], 1, mean)
Please note, this is more flexible as you can also quickly adapt to a number of other cases. as an example, to get the maximum value by row you can apply the max function by row to your dataframe:
DF$max <- apply(DF[,2:ncol(DF)], 1, max)
Upvotes: 0
Reputation: 193
Here is a dplyr solution:
library(tidyverse)
DF %>%
rowwise() %>%
mutate(mean = mean(c(C1, C2), na.rm=TRUE))
Upvotes: 1
Reputation: 26238
This will give you mean of every even numbered column if we leave ID
column.
DF$mean_1 <- rowMeans(DF[(seq_along(DF)[(seq_along(DF) %% 2 == 1)])][-1])
ID C1 C2 C3 C4 C5 C6 C7 C8 mean_1
1 A 3 3 5 5 5 5 5 5 4.50
2 B 2 7 4 4 6 5 6 7 5.75
3 C 4 3 3 3 3 3 3 3 3.00
4 D 4 4 6 6 6 6 6 6 5.50
5 E 5 5 3 3 2 4 1 2 3.50
Upvotes: 0
Reputation: 389275
You can use rowMeans
for every 2nd column as :
DF$mean_1 <- rowMeans(DF[seq(3, ncol(DF), 2)], na.rm = TRUE)
DF
# ID C1 C2 C3 C4 C5 C6 C7 C8 mean_1
#1 A 3 3 5 5 5 5 5 5 4.50
#2 B 2 7 4 4 6 5 6 7 5.75
#3 C 4 3 3 3 3 3 3 3 3.00
#4 D 4 4 6 6 6 6 6 6 5.50
#5 E 5 5 3 3 2 4 1 2 3.50
This gives mean of C2
, C4
, C6
and C8
. I am not sure if you need another column called mean_2
which would be mean of C1
, C3
, C5
and C7
?
Upvotes: 0