[R] A function that can modify an object? Or at least shows principles how to modify an object?

Rui Barradas ruipbarradas at sapo.pt
Thu May 16 18:54:20 CEST 2013


Hello,

If I understood it correctly, the following should do it.


fun1 <- function(x, what, map, value){
	y <- x[[what]]
	i1 <- seq_len(map - 1)
	i2 <- seq_along(y)[-i1]
	x[[what]] <- c(y[i1], value, y[i2])
	x
}
fun2 <- function(x, what, map, value){
	y <- x[[what]]
	i1 <- seq_len(map - 1)
	i2 <- seq_along(y)[-i1]
	x[[what]] <- rbind(y[i1, ], value, y[i2, ])
	x
}

map <- UUU$my.table["bbb8888", "map"]  # Not sure about this
UUU[2] <- fun1(UUU[2], "scores", map, NA)
UUU[2] <- fun2(UUU[2], "cov", map, c(1, 1, 1))
UUU[2] <- fun1(UUU[2], "maf", map, 0)




Note however that it is not tested, since the example you've given is 
not very easy to recreate. Use ?dput to post data examples.

dput(UUU[2])  # paste the output of this in a post


Hope this helps,

Rui Barradas

Em 16-05-2013 17:23, Aldi escreveu:
> Hi, If I have an R object UUU, where the second element is U2, based on
> "g" column of my.table
>
> my.table of UUU is:
> mmm         ggg   gindex   map       Info
> aaa123      U1       1           1             1
> aaa124      U1       1           2             1
> bbb1378     U2       2           1             1
> bbb8888     U2       2           2             0
> bbb1389     U2       2           3             1
> ccc2222     Z3       3           1             1
> ccc33333    Z3       3           2             0
> ccc4444     Z3       3           3             1
> ccc55555    Z3       3           4             0
>
> Before (see Before) the analysis I had dropped those "mmm" rows from
> my.table of UUU that had Info==0, thus not included in the analysis.
> After (see After) analysis found that I need to add bbb8888 (in the
> second position, as defined by map) for U2, ccc3333 and ccc5555 for Z3
> and s.o.
> The bbb8888, does not have any info in general, but a collaborator is
> saying that he needs the bbb8888 for documentation in the UUU object.
> A solution is to rerun the analysis, but the list is very long, the
> analysis will take 1 day. Is there any ready function that can modify an
> object? Or at least shows principles how to modify an object?
> I have labeled elements of U2 as (e) which needs an NA in the second
> position; (d) needs a name bbb8888 as a second column and as a second
> row and correspondingly in its rows and columns use value 1.0; NO change
> for (c); adding 0.0 in the second position for (b); and NO change for
> (a). (see After) Thank you in advance, Aldi
>
> Before:
> =======
>   > UUU[2]
> $U2
> $U1$scores (e)
> [1]  -1.946707 -57.970488
>
> $U2$cov (d)
>                           bbb1378       bbb1379
> bbb1378  1.10362564 -0.01222695
> bbb1379 -0.01222695 26.88805020
>
> $U2$n (c)
> [1] 1802
>
> $U1$maf   (b)
> [1] 0.0002774695 0.0077691454
>
> $U2$sey (a)
> [1] 13.3867
>
> After:
> =====
>   > UUU[2]
> $U2
> $U2$scores (e)
> [1]  -1.946707 NA -57.970488
>
> $U2$cov (d)
>                           bbb1378  bbb8888     bbb1379
> bbb1378  1.10362564  1.0  -0.01222695
> bbb8888  1.0  1.0  1.0
> bbb1379 -0.01222695  1.0  26.88805020
>
> $U2$n (c)
> [1] 1802
>
> $U2$maf   (b)
> [1] 0.0002774695  0.0  0.0077691454
>
> $U2$sey (a)
> [1] 13.3867
>



More information about the R-help mailing list