Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i need to check if an list element exists in R and if not, i need to create one. This happens inside a function.

I have a named list (currently with empty lists):

> comparisonLog
$`01-response.json`
NULL

$`02-response.json`
NULL

$`03-response.json`
NULL

$`04-response.json`
NULL

$`05-response.json`
NULL

i am interested in the third element:

elementOfInterest <- responseIterator[[3]]
> elementOfInterest
[3] "03-response.json"

Why am i getting en error here?

exists("price", where = comparisonLog[[elementOfInterest]])
Error in as.environment(where) : using 'as.environment(NULL)' is defunct

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
994 views
Welcome To Ask or Share your Answers For Others

1 Answer

Perhaps you should use comparisonLog[[responseIterator]] for the argument where, rather than comparisonLog[[responseIterator]][[]]

> exists("price", where = comparisonLog[[3]])
[1] TRUE

Dummy Data

> dput(comparisonLog)
list(A = list(price = 1), B = list(price = 1), C = list(price = 1), 
    D = list(price = 1), E = list(price = 1))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...