Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
995 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...