[R] Why do return or visible don´t return my objekt?
Christoph Heibl
christoph.heibl at gmx.net
Fri Jan 26 21:40:54 CET 2007
Dear Marc, dear Weiwei,
Thanks a lot!
As I now see me question was quite silly, but we´re always much
smarter with hindsight...
: )
Best,
Christoph
On 26.01.2007, at 21:16, Marc Schwartz wrote:
> 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!
>>
>> Christoph
>
> invisible() 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
>
More information about the R-help
mailing list