0

I have created the following data frame

 df<-data.frame("A"<-c(1:20), "B"<-c(21:40),"C"<-c(11:30))
 names(df)<-c("A", "B", "C")
 nameslist<-c("A", "B")

I am trying to obtain the mean and weighted means for both columns combined

I have tried this

   mean(df[,names(df)[names(df)%in%nameslist]])

I get the following error.

Warning message:
In mean.default(df[, names(df)[names(df) %in% nameslist]]) :
  argument is not numeric or logical: returning NA

I have also tried computing the weighted mean as follows

 weighted.mean(df[,names(df)[names(df)%in%nameslist]])
 410

I am getting an output of 410 in this case. I am unable to uncover where I am wrong. I request someone to guide me here

Raghavan vmvs
  • 1,213
  • 1
  • 10
  • 29
  • 1
    Extract columns from `df` passing name vector and calculate mean on matrix with: `mean(t(df[, nameslist]))` – pogibas May 25 '18 at 07:27
  • 1
    `df <- data.frame(A=c(1:20), B=c(21:40), C=(11:30)); sapply(df[, c("A", "B")], mean)` – jogo May 25 '18 at 07:38

1 Answers1

0

To obteain the global mean of columns A and B, try this code:

mean(unlist(df[,which(colnames(df)%in%nameslist)]))
[1] 20.5

About weighted.mean you have first to define w:

w   
a numerical vector of weights the same length as x giving the weights to use for elements of x.

An example:

w<-c(0.5,1)   
weighted.mean(unlist(df[,which(colnames(df)%in%nameslist)]),c(rep(w[1],nrow(df)),rep(w[2],nrow(df))))
        [1] 23.83333

Update: if you want to derive weights based on the number of rows with values greater than zero, a solution:

w<-c(mean(df[,1]>0),mean(df[,2]>0))

Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39