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

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

checking if a list element exists in R with exists() function

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

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

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...