# [R] grep problem decimal points looping

David Winsemius dwinsemius at comcast.net
Tue Aug 10 17:31:45 CEST 2010

On Aug 10, 2010, at 11:14 AM, RCulloch wrote:

>
> Hi David,
>
> Thanks very much for that reply! I might be a touch out of my
> comfort zone,
> but I can see how the loop script works and where I went wrong, but
> I'm not
> sure if I am asking the correct questions here, or perhaps more
> accurately
> I'm using the wrong command for the task in question - and as you
> say more
> info would be better! So.....
>
> I want to split the data by day to look at the proportion of time an
> individual spent in each of the eight behaviours - there are 30 rows
> (i.e.
> individuals).
>
> So I'm going over old code trying to make it better (not that it
> could be
> worse!), especially trying to make it more efficient!
>
> So my old code did this (manually for each day):
>
> ##DAY1##
> DAY1 <-cbind(\$X1.1, scananal\$X2.1, scananal\$X3.1, scananal\$X4.1,
> scananal\$X5.1, scananal\$X6.1, scananal\$X7.1, scananal\$X8.1)

From our earlier efforts:
> DAY[[1]]
[1] 1 2 3 4 5 6 7 8

If you were using the earlier results, then this could be made easier
and more generalizable to an iterative process by:

DAY1cols <- scananal[ , DAY[[1]] ]   or just ...

DAY1cols <- scananal[  DAY[[1]] ]

You could replace the 1 by an i in a for loop, or create a list of
"subsets" with lapply.

>
> which would give, for example,
>
>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]   14    0    2    1    2    2    0    3
> [2,]   23    0    0    1    0    0    0    0
> [3,]    0    0    0    0    0    0    0    0
> [4,]    0    0    0    0    0    0    0    0
> [5,]    0    0    0    0    0    0    0    0
> [6,]    0    0    0    0    0    0    0    0
>
>
>
> I'd run the following script to get the proportions then bind that
> together
> with other data
>
>
>
> ###~~~~DAY1~~~###
>
> ## CALC NSCANS PER ID ##
> n <- rowSums(DAY1)
>
> ## GIVE THE DAY NUMBER TO THE DATAFILE
> DAY <- rep(1,30)
>
> ## CALC PROPORTION OF TIME IN EACH ACTIVITY ##
> scansprop <- as.data.frame(prop.table(DAY1,1))
>
> ##CALC AS ARC_SINE_TRANSFORMED###
> transscan<-asin(scansprop)
> names(transscan)
>
> ##CHECK IT ALL ADDS TO ONE!! ##
> rowSums(scansprop)
>
> ##MERGES ALL THE DATA FOR THE DAY
> DAY1_SUM <- cbind(n,DAY,DAY1,scansprop,transscan)
>
>
>
> Then I would merge each of the days, so this script works, but I
> know it is
> rather a poor effort in R script to say the least!

That may work perfectly well. I don't know. I didn't follow the logic
unambiguous definition of a denominator in several locations.

>
> I'm trying to work through this myself, but hit a hurdle in the first
> instance!
>
> Not sure if this is any clearer?

Perhaps. Two options: Try to put that sequence into a function that
will return a structure. Then test it. Then lapply it.

Or put it into sequence that makes DAY1_SUM inside a for-loop, take
the results of for() {} on DAY[[i]]  and have it assign values to a
structure like a list or a matrix.

--

David Winsemius, MD
West Hartford, CT