Search code examples
rslash

how to add up values before and after a slash


Hi I have a question related to slash.

the data I'm working on is like:

X Y Z
12/22 14/32 22/34

I would like to get the mean of the values before and after the slash.

X Y Z
17 18 28

How can I do this?


Solution

  • To get the mean of each cell even if there are more than one row in the data.frame:

    as.data.frame(
      apply(dat, 1:2, function(x) {
          mean(as.integer(unlist(strsplit(x,"/"))))
        }
      )
    )