How to use apply group of function in R to calculate mean of values with plus delimiter

MAPK

I have a data like this

tt<- structure(list(Time = c(48L, 48L, 72L, 72L), WT_H20 = structure(c(13L, 
11L, 17L, 14L), .Label = c("0", "0.2+0.2", "0.5+0.4", "0.5+0.6", 
"0.8+0.85", "1.2+1.3", "1.3+1.35", "1.5+1.1", "1.5+1.2", "1.6+2", 
"1.7+1.5", "1.8+1.5", "1.9+1.7", "1.9+2.1", "2.1+1.7", "2.3+2.7", 
"2.8+2.8", "2.9+2.2"), class = "factor"), WT_Ago2_800 = structure(c(1L, 
1L, 4L, 5L), .Label = c("0", "0.1+0.1", "0.5+0.1", "0.5+0.5", 
"0.8+0.8"), class = "factor"), WT_Ago2_400 = structure(c(14L, 
8L, 4L, 15L), .Label = c("0", "0.1+0.1", "0.2+0.2", "0.5+0.5", 
"0.6+0.55", "0.7+0.6", "0.75+0.7", "0.8+0.8", "0.9+0.8", "0.9+0.9", 
"1.1+1.1", "1.35+1.3", "1.6+1.7", "1+1", "2+2.4"), class = "factor")), row.names = 17:20, class = "data.frame")

I want to string split and get the average of values within cells with+. I have my code that does this for one column sapply(strsplit(as.character(tt$WT_H20), "\\+"), function(x) mean(as.numeric(x))) , but I want to do this for all columns using apply group of functions. I can do this with loops, but wanted to use apply functions.

d.b

lapply loops through the columns of tt. grepl checks if the columns have "+". If it does, split at "+", convert to numeric, and take mean.

data.frame(lapply(tt, function(x){
    if (any(grepl("\\+", x))){
        sapply(strsplit(as.character(x), "\\+"), function(y) mean(as.numeric(y)))
    }else{
        x
    }
}))
#  Time WT_H20 WT_Ago2_800 WT_Ago2_400
#1   48    1.8         0.0         1.0
#2   48    1.6         0.0         0.8
#3   72    2.8         0.5         0.5
#4   72    2.0         0.8         2.2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Python

How to apply a function by group?

From Dev

R How to use apply with ifelse function to search for string values over many columns?

From Dev

How to calculate mean values from a linear model in R?

From Dev

in R, how to calculate mean of all column, by group?

From Dev

how to use group_by in a function in R

From Dev

R: Apply function to calculate mean of a single column of dataframe across a list

From Dev

How to calculate mean spatial location by group

From Dev

How to use apply function to calculate the distance between two matrices

From Dev

pandas how to use apply function to check if 2 values exist

From Dev

How to calculate mean of every three values of a list

From Dev

How to use the apply function to find the mean of array entry in the same position in R

From Dev

How to apply/loop the same function to a group of similar objects in R

From Dev

Using apply function to calculate the mean of a column

From Dev

How do you index and use current and previous column values to calculate the next column value in a pandas.apply function?

From Dev

How to find grid neighbours (x, y as integers) group them and calculate mean of their values in spark

From Dev

How to calculate mean of a list without apply

From Dev

how to use apply function to take no names column mean?

From Dev

How to group by multiple column values in pandas and apply a ifelse to impute/calculate values

From Dev

How to Calculate mean, median, max and min values for group by 15 days without outlier effect in R

From Dev

Subset / group by pandas Data Frame to calculate mean and apply to missing values

From Dev

How to calculate mean in group by another group?

From Dev

how to apply a function to each group

From Dev

How to calculate mean of values per unique class

From Dev

R calculate how many values used to calculate mean in aggregate function

From Dev

How to group specific items in a column and calculate the mean

From Dev

How to use group by and a conditional mean at the same time in R?

From Dev

How to calculate the mean of a new group?

From Dev

Combine apply function with lapply: calculate mean of groups in df

From Dev

How to calculate a mean by group in a JS Array?

Related Related

  1. 1

    How to apply a function by group?

  2. 2

    R How to use apply with ifelse function to search for string values over many columns?

  3. 3

    How to calculate mean values from a linear model in R?

  4. 4

    in R, how to calculate mean of all column, by group?

  5. 5

    how to use group_by in a function in R

  6. 6

    R: Apply function to calculate mean of a single column of dataframe across a list

  7. 7

    How to calculate mean spatial location by group

  8. 8

    How to use apply function to calculate the distance between two matrices

  9. 9

    pandas how to use apply function to check if 2 values exist

  10. 10

    How to calculate mean of every three values of a list

  11. 11

    How to use the apply function to find the mean of array entry in the same position in R

  12. 12

    How to apply/loop the same function to a group of similar objects in R

  13. 13

    Using apply function to calculate the mean of a column

  14. 14

    How do you index and use current and previous column values to calculate the next column value in a pandas.apply function?

  15. 15

    How to find grid neighbours (x, y as integers) group them and calculate mean of their values in spark

  16. 16

    How to calculate mean of a list without apply

  17. 17

    how to use apply function to take no names column mean?

  18. 18

    How to group by multiple column values in pandas and apply a ifelse to impute/calculate values

  19. 19

    How to Calculate mean, median, max and min values for group by 15 days without outlier effect in R

  20. 20

    Subset / group by pandas Data Frame to calculate mean and apply to missing values

  21. 21

    How to calculate mean in group by another group?

  22. 22

    how to apply a function to each group

  23. 23

    How to calculate mean of values per unique class

  24. 24

    R calculate how many values used to calculate mean in aggregate function

  25. 25

    How to group specific items in a column and calculate the mean

  26. 26

    How to use group by and a conditional mean at the same time in R?

  27. 27

    How to calculate the mean of a new group?

  28. 28

    Combine apply function with lapply: calculate mean of groups in df

  29. 29

    How to calculate a mean by group in a JS Array?

HotTag

Archive