[R] function using values separated by a comma
jim holtman
jholtman at gmail.com
Mon Oct 18 15:57:33 CEST 2010
Try this (I think your result in [2,2] is incorrect):
> dat <- read.table(tc <- textConnection(
+ '0,1 1,3 40,10 0,0
+ 20,5 4,2 10,40 10,0
+ 0,11 1,2 120,10 0,0'), as.is = TRUE)
> closeAllConnections()
> # split the data and create new matrix
> newDat <- lapply(dat, function(.col){
+ # split by comma, unlist, convert to numeric and divide
+ x1 <- matrix(as.numeric(unlist(strsplit(.col, ','))), nrow = 2)
+ x1[1, ] / colSums(x1)
+ })
> do.call(cbind, newDat)
V1 V2 V3 V4
[1,] 0.0 0.2500000 0.800000 NaN
[2,] 0.8 0.6666667 0.200000 1
[3,] 0.0 0.3333333 0.923077 NaN
>
On Mon, Oct 18, 2010 at 2:37 AM, burgundy <sauburn at yahoo.com> wrote:
>
> Hi,
>
> Thanks again for your help with this. I would like to use a variation of
> this function in a similar dataset (numeric) with elements separated by a
> comma e.g.
>
> dat <- read.table(tc <- textConnection(
> '0,1 1,3 40,10 0,0
> 20,5 4,2 10,40 10,0
> 0,11 1,2 120,10 0,0'), sep="")
>
> to simply calculate the frequency of the first number divided by the total
> number, i.e. x[1]/sum(x).
>
> to produce:
>
> [,1] [,2] [,3] [,4]
> [1,] 0 0.25 0.8 NaN
> [2,] 0.8 0.33 0.2 1
> [3,] 0 0.33 0.92 NaN
>
>
> My actual dataset is an enormous file (800,000 rows and 100 columns). Any
> advice on how I can do this, maybe using gsubfn?
>
> Thank you very much!
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/function-using-values-separated-by-a-comma-tp2967870p2999723.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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list