Christoph Heibl
2007-Jan-26 19:23 UTC
[R] Why do return or visible don´t return my objekt?
Dear RRRRRrrrrrrrrlist!
I?ve got two lists which contain sets of DNA-sequences. They look
something like this:
List of 33
$ Cunonia_atrorubens : chr [1:247] "t" "t"
"n" "t" ...
$ Cunonia_balansae : chr [1:254] "t" "c"
"c" "c" ...
$ Cunonia_capensis : chr [1:236] "v" "t"
"c" "c" ...
$ Cunonia_macrophylla : chr [1:236] "t" "t"
"a" "t" ...
$ Pancheria_engleriana : chr [1:315] "c" "t"
"c" "c" ...
[....]
$ Brunellia_colombiana : chr [1:336] "t" "t"
"c" "t" ...
$ Brunellia_oliveri : chr [1:368] "t" "a"
"a" "t" ...
$ Bauera_rubioides : chr [1:276] "t" "c"
"t" "c" ...
$ Bauera_sessiliflora : chr [1:332] "t" "c"
"t" "c" ...
$ Davidsonia_pruriens_var._jeryseyana: chr [1:300] "t" "t"
"c" "t" ...
$ Davidsonia_sp._'johnsonii' : chr [1:268] "c"
"c" "c" "t" ...
- attr(*, "species")= chr [1:33] "Cunonia_atrorubens"
"Cunonia_balansae" "Cunonia_capensis"
"Cunonia_macrophylla" ...
The names of each entry in first list match exactly the names of the
entries in the second list and both list contain the same number of
sequences (although not of the same length)
I want to concatenate these sequences by the following function:
interleave <- function(seq1, seq2)
{
#create a list to contain concatenated sequences
sequence <- list()
for (name in names(seq1)) # get names as indices
{
# concatenate sequences
sequence[[name]] <- c(seq1[[name]], seq2[[name]])
}
# return concatenated sequences
invisible(sequence)
}
I guess the function works in principle, because they commands do so
if I enter them step by step.
But when I call the function it does not return my object "sequence".
But why?
(This happened to me with some functions that I wrote, but not all.
The error seems quite erratic : ) to me and I can?t figure out what
to change)
Thanks a lot in advance!
Christoph
On Fri, 2007-01-26 at 20:23 +0100, Christoph Heibl wrote:> Dear RRRRRrrrrrrrrlist!Ruh-Ro.... ;-)> I?ve got two lists which contain sets of DNA-sequences. They look > something like this: > > List of 33 > $ Cunonia_atrorubens : chr [1:247] "t" "t" "n" "t" ... > $ Cunonia_balansae : chr [1:254] "t" "c" "c" "c" ... > $ Cunonia_capensis : chr [1:236] "v" "t" "c" "c" ... > $ Cunonia_macrophylla : chr [1:236] "t" "t" "a" "t" ... > $ Pancheria_engleriana : chr [1:315] "c" "t" "c" "c" ... > [....] > $ Brunellia_colombiana : chr [1:336] "t" "t" "c" "t" ... > $ Brunellia_oliveri : chr [1:368] "t" "a" "a" "t" ... > $ Bauera_rubioides : chr [1:276] "t" "c" "t" "c" ... > $ Bauera_sessiliflora : chr [1:332] "t" "c" "t" "c" ... > $ Davidsonia_pruriens_var._jeryseyana: chr [1:300] "t" "t" "c" "t" ... > $ Davidsonia_sp._'johnsonii' : chr [1:268] "c" "c" "c" "t" ... > - attr(*, "species")= chr [1:33] "Cunonia_atrorubens" > "Cunonia_balansae" "Cunonia_capensis" "Cunonia_macrophylla" ... > > > The names of each entry in first list match exactly the names of the > entries in the second list and both list contain the same number of > sequences (although not of the same length) > I want to concatenate these sequences by the following function: > > interleave <- function(seq1, seq2) > { > #create a list to contain concatenated sequences > sequence <- list() > for (name in names(seq1)) # get names as indices > { > # concatenate sequences > sequence[[name]] <- c(seq1[[name]], seq2[[name]]) > } > # return concatenated sequences > invisible(sequence) > } > > I guess the function works in principle, because they commands do so > if I enter them step by step. > But when I call the function it does not return my object "sequence". > But why? > (This happened to me with some functions that I wrote, but not all. > The error seems quite erratic : ) to me and I can?t figure out what > to change) > > Thanks a lot in advance! > > Christophinvisible() does just what is says: Details This function can be useful when it is desired to have functions return values which can be assigned, but which do not print when they are not assigned. You would have to assign the result of your interleave() function to another object: Res <- interleave(..., ...) However, there is an easier way to accomplish what you want: L1 <- as.list(head(iris, 6)) L2 <- as.list(head(iris, 3))> L1$Sepal.Length [1] 5.1 4.9 4.7 4.6 5.0 5.4 $Sepal.Width [1] 3.5 3.0 3.2 3.1 3.6 3.9 $Petal.Length [1] 1.4 1.4 1.3 1.5 1.4 1.7 $Petal.Width [1] 0.2 0.2 0.2 0.2 0.2 0.4 $Species [1] setosa setosa setosa setosa setosa setosa Levels: setosa versicolor virginica> L2$Sepal.Length [1] 5.1 4.9 4.7 $Sepal.Width [1] 3.5 3.0 3.2 $Petal.Length [1] 1.4 1.4 1.3 $Petal.Width [1] 0.2 0.2 0.2 $Species [1] setosa setosa setosa Levels: setosa versicolor virginica # Use mapply() to concatenate the two lists Res <- mapply(c, L1, L2, SIMPLIFY = FALSE)> Res$Sepal.Length [1] 5.1 4.9 4.7 4.6 5.0 5.4 5.1 4.9 4.7 $Sepal.Width [1] 3.5 3.0 3.2 3.1 3.6 3.9 3.5 3.0 3.2 $Petal.Length [1] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.4 1.3 $Petal.Width [1] 0.2 0.2 0.2 0.2 0.2 0.4 0.2 0.2 0.2 $Species [1] 1 1 1 1 1 1 1 1 1 See ?mapply HTH, Marc Schwartz
?invisible if you return in this function with invisible, then you need to assign like this> x0 <- lapply(1:3, function(i) (c("0","1"))) > names(x0) <- c("a", "b", "c") > x0$a [1] "0" "1" $b [1] "0" "1" $c [1] "0" "1"> x1 <- lapply(1:3, function(i) (c("2","3"))) > names(x1) <- c("a", "b", "c")> x2 <- interleave(x0, x1) > x2$a [1] "0" "1" "2" "3" $b [1] "0" "1" "2" "3" $c [1] "0" "1" "2" "3" otherwise, you need to remove invisible when you return, like this> interleave(x0, x1)$a [1] "0" "1" "2" "3" $b [1] "0" "1" "2" "3" $c [1] "0" "1" "2" "3" then you also assign like x2 <- interleave(x0, x1) HTH, weiwei On 1/26/07, Christoph Heibl <christoph.heibl at gmx.net> wrote:> Dear RRRRRrrrrrrrrlist! > > I?ve got two lists which contain sets of DNA-sequences. They look > something like this: > > List of 33 > $ Cunonia_atrorubens : chr [1:247] "t" "t" "n" "t" ... > $ Cunonia_balansae : chr [1:254] "t" "c" "c" "c" ... > $ Cunonia_capensis : chr [1:236] "v" "t" "c" "c" ... > $ Cunonia_macrophylla : chr [1:236] "t" "t" "a" "t" ... > $ Pancheria_engleriana : chr [1:315] "c" "t" "c" "c" ... > [....] > $ Brunellia_colombiana : chr [1:336] "t" "t" "c" "t" ... > $ Brunellia_oliveri : chr [1:368] "t" "a" "a" "t" ... > $ Bauera_rubioides : chr [1:276] "t" "c" "t" "c" ... > $ Bauera_sessiliflora : chr [1:332] "t" "c" "t" "c" ... > $ Davidsonia_pruriens_var._jeryseyana: chr [1:300] "t" "t" "c" "t" ... > $ Davidsonia_sp._'johnsonii' : chr [1:268] "c" "c" "c" "t" ... > - attr(*, "species")= chr [1:33] "Cunonia_atrorubens" > "Cunonia_balansae" "Cunonia_capensis" "Cunonia_macrophylla" ... > > > The names of each entry in first list match exactly the names of the > entries in the second list and both list contain the same number of > sequences (although not of the same length) > I want to concatenate these sequences by the following function: > > interleave <- function(seq1, seq2) > { > #create a list to contain concatenated sequences > sequence <- list() > for (name in names(seq1)) # get names as indices > { > # concatenate sequences > sequence[[name]] <- c(seq1[[name]], seq2[[name]]) > } > # return concatenated sequences > invisible(sequence) > } > > I guess the function works in principle, because they commands do so > if I enter them step by step. > But when I call the function it does not return my object "sequence". > But why? > (This happened to me with some functions that I wrote, but not all. > The error seems quite erratic : ) to me and I can?t figure out what > to change) > > Thanks a lot in advance! > > Christoph > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III