str_rst
str_rst

Reputation: 175

calculating average of two column in another column

I am trying to calculate the average of columns in another column but getting errror

converted all the nastring na.strings = c("N") to NA but after the class of columns is character.

after this i have NA in place of N in data frame but still the class of column is character

df <- data.frame("T_1_1"= c(68,24,"N",105,58,"N",135,126,24),
                 "T_1_2"=c(26,105,"N",73,39,97,46,108,"N"),
                 "T_1_3"=c(93,32,73,103,149,"N",147,113,139),
                 "S_2_1"=c(69,67,94,"N",77,136,137,92,73),
                 "S_2_2"=c(87,67,47,120,85,122,"N",96,79),
                 "S_2_3"= c(150,"N",132,121,29,78,109,40,"N"),
                 "TS1_av"=c(68.5,45.5,94,105,67.5,136,136,109,48.5),
                 "TS2_av"=c(56.5,86,47,96.5,62,109.5,46,102,79),
                 "TS3_av"=c(121.5,32,102.5,112,89,78,128,76.5,139)

)

df$TS1_av <- rowMeans(df[,c(as.numeric(as.character("T_1_1","S_2_1")))], na.rm=TRUE)

Upvotes: 0

Views: 211

Answers (3)

Ronak Shah
Ronak Shah

Reputation: 388797

You can use :

#Change 'N' to NA
df[df == 'N'] <- NA
#Change the type of columns
df <- type.convert(df, as.is = TRUE)
#Take mean of selected columns and add a new column
df$TS1_av <- rowMeans(df[,c("T_1_1","S_2_1")], na.rm=TRUE)
df

Upvotes: 1

Waldi
Waldi

Reputation: 41210

You could use readr::parse_number to extract numbers and replace any string that can't be converted to numeric by NA.
the na argument allows to specify strings to be interpreted as NA (here 'N'). If you don't supply this argument, you get a warning for every string which couldn't be interpreted but it's also replaced by NA.

library(dplyr)
library(readr)

df <- df %>% mutate(across(where(is.character),readr::parse_number,na='N'))   
df$TS1_av <- rowMeans(df[,c("T_1_1","S_2_1")], na.rm=TRUE)
df

Upvotes: 0

hello_friend
hello_friend

Reputation: 5788

2 Base R solutions:

# Columns to subset out: cols => character vector
cols <- c("T_1_1", "S_2_1")

# Option 1: calculate the mean row-wise: TS1_av => numeric vector
df$TS1_av <- apply(df[,cols], 1, function(x){
  mean(suppressWarnings(as.numeric(x)), na.rm = TRUE)
  }
)

# Columns to subset out: cols => character vector
cols <- c("T_1_1", "S_2_1")

# Option 2: Coerce to numeric and calculate the row mean:
# TS1_av => numeric vector
df$TS1_av <- rowMeans(
  suppressWarnings(
    vapply(df[,cols], as.numeric, numeric(nrow(df)))
  ),
  na.rm = TRUE
)

Upvotes: 0

Related Questions