[R] function using values separated by a comma

Joshua Wiley jwiley.psych at gmail.com
Fri Oct 8 09:54:28 CEST 2010


Hi,

It is not the most elegant thing ever, but this does what you want.  I
am *fairly* certain it generalizes to different sized matrices, but
I'd double check.  When you divide by 0, it returns NaN, but this is
pretty easy to fix if you really want 0s using is.nan().  My general
process was: split data by commas, convert to numeric, define a
function that does your calculations, apply this function, convert
results back from a list to a matrix with the same number of columns
as the original data, add any column/rownames from original matrix,
return results.


# Define a function
my.fun <- function(dat) {
  # split data by commas, and convert to numeric
  # with commas, it would have been character
  # so something like this is necessary
  temp <- lapply(strsplit(dat, ","), as.numeric)
  # Define summary function
  my.summary <- function(x) {
    ## This combines your first and second steps
    value <- x[1]/sum(x)
    ## if value > .5, return 1 - value
    ## otherwise, just return the value
    if(isTRUE(value > 0.5)) {
      return(1 - value)
    } else {return(value)}
  }
  temp2 <- lapply(temp, my.summary)
  output <- matrix(unlist(temp2), ncol = ncol(dat),
    dimnames = dimnames(dat))
  return(output)
}

# Create your data
dat <- c("0,1", "1,3", "40,10", "0,0", "20,5", "4,2",
         "10,40", "10,0", "0,11", "1,2", "120,10", "0,0")
dat <- matrix(dat, ncol = 4, byrow = TRUE)

# Test it out
my.fun(dat)

HTH,

Josh

On Thu, Oct 7, 2010 at 10:19 PM, burgundy <sauburn at yahoo.com> wrote:
>
> Hello,
>
> I have a dataframe (tab separated file) which looks like the example below -
> two values separated by a comma, and tab separation between each of these.
>
>     [,1]  [,2]  [,3]  [ ,4]
> [1,] 0,1  1,3   40,10  0,0
> [2,] 20,5  4,2  10,40  10,0
> [3,] 0,11  1,2  120,10  0,0
>
> I would like to calculate the percentage of the smallest number separated by
> the comma by:
> 1) summing the values e.g. for [1,3] where 40,10, 40+10 = 50
> 2) taking the first value and dividing it by the total e.g. for [1,3], 40/50
> = 0.8
> 3) where the value generated by 2) is >0.5, print 1-value, otherwise, leave
> value e.g. for [1,3], where value is 0.8, print 1-0.8 = 0.2
>
> plan to generate file like:
>
>    [,1]  [,2]  [,3]  [,4]
> [1,] 1   0.25  0.2  0
> [2,] 0.2  0.33  0.2  1
> [3,] 1  0.33  0.08  0
>
> Apologies, I know this is very complex. Any help, even just some pointers on
> how to write a general function where values are separated by a comma, is
> realy very much appreciated!
>
> Thank you
>
> --
> View this message in context: http://r.789695.n4.nabble.com/function-using-values-separated-by-a-comma-tp2967870p2967870.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list