[R] Extracting Hash via Vector

Gundala Viswanath gundalav at gmail.com
Tue Jan 13 15:45:26 CET 2009


Thanks for your most reasonable reply, Henrique.

- Gundala Viswanath
Jakarta - Indonesia



On Tue, Jan 13, 2009 at 8:01 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> Try this:
>
> unlist(ifelse(q %in% names(x), x[q], NA))
>
> On Tue, Jan 13, 2009 at 8:49 AM, Gundala Viswanath <gundalav at gmail.com>
> wrote:
>>
>> Dear all,
>>
>> Suppose I have a hash created with this
>>
>>  x <- list()
>> for (i in c('test', 'some', 'more')){
>>   x[[i]] <- runif(1)
>> }
>>
>> then I want to extract the elem of that hash with
>> a vector
>>
>> > q <- c("some", "more", "not_there")
>>
>> But why this failed?
>>
>> > extracted <- x[[q]]
>> Error in x[[q]] : subscript out of bounds
>>
>> we expect the output of 'extracted' to be
>> a vector as well. When the key is not present
>> to give "NA" in vector
>>
>> - Gundala Viswanath
>> Jakarta - Indonesia
>>
>> ______________________________________________
>> 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.
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>




More information about the R-help mailing list