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?
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,"/")))) } ) )
values<-c("12/22","14/32","22/34") > unlist(lapply(strsplit(values,"/"),function(values2){mean(as.numeric(values2))})) [1] 17 23 28
Another solution:
as.data.frame(lapply(dat, function(x) mean(c(as.integer(sub("/.+", "", x)), as.integer(sub(".+/", "", x))))))
where dat is the name of your data frame.
dat
To get the mean of each cell even if there are more than one row in the data.frame: