-1

I have two lists that I'm getting from an API. I need to compare the two lists in R to determine which items are present in both lists. I had hoped to do this with the intersect() command, but it did not work. Upon further inspection, I noticed that each list was actually a single vector comprising multiple items separated by commas and escape characters. Is it possible to transform these vectors into multi-item lists so that I can compare lists? Here is some example code:

What I'd like:

 > intersect(x,y)
 [[1]]
 [1] "c"

What I'm seeing instead:

 > intersect(x,y)
 list()

 > as.character(x)
 c(\"a\", \"b\", \"c\")

 > as.character(x)
 c(\"c\", \"d\", \"e\")

What's going on here? How do I compare x and y? Is there a way to transform these vectors into lists so that I can use the intersect() command?

edit: refined example and clarified data source

user3786999
  • 1,037
  • 3
  • 13
  • 24
  • 1
    Would you show the output of `dput(x)` and `str(x)` as well? I'm just curious if `x` really is a character vector to start with or if the `as.character` is coercing it to character. – Aaron left Stack Overflow Sep 03 '17 at 03:12
  • I can't replicate your error. The list version worked fine based on this code. Though as Ryan Runge pointed out in the answer, you should be using `c()` rather than `list()` – Z.Lin Sep 03 '17 at 03:13
  • And is that really the exact output of `as.character`? Usually it would start with `[1]` and have the strings in quotations. – Aaron left Stack Overflow Sep 03 '17 at 03:17
  • You haven't provided enough information either in your question or in youra answer below to reproduce the problem; if you don't provide it we'll close the question as it's not helpful to anyone else. I hope you will provide more as there were some interesting points in the question that I'm still curious about. – Aaron left Stack Overflow Sep 05 '17 at 15:15

2 Answers2

2

I'm still guessing here, as you haven't responded to my questions, but the only way I see for you to be getting output like that is if x and y are lists with the first element actually containing the R code that you would use to create the vector you want, like this.

x <- list('c("a", "b", "c")')
y <- list('c("c", "d", "e")')
intersect(x, y)
## list()
as.character(x)
## [1] "c(\"a\", \"b\", \"c\")"
as.character(y)
## [1] "c(\"c\", \"d\", \"e\")"

If so, what you need to do is to evaluate these expressions, and then you'll have the vectors that you think you have.

xx <- eval(parse(text=x[[1]]))
yy <- eval(parse(text=y[[1]]))
xx
## [1] "a" "b" "c"
yy
## [1] "c" "d" "e"
intersect(xx, yy)

Ryan Runge suggests that "Having extra quotes like this can happen more often as data is shared between different languages or softwares. So it could be an unintended effect of how the API is being accessed." (Thanks!)

This doesn't however, work with the explanation you gave. More information is needed!

Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
-1

Thanks for the advice, everyone. I was able solve this problem like this:

 intersect(as.list(as.character(x[[1]])),as.list(as.character(y[[1]])))

I don't really understand why putting the term [[1]] after each list name solves the issue, but it seems to nonetheless.

user3786999
  • 1,037
  • 3
  • 13
  • 24
  • That's not enough information to reproduce the problem; if you don't provide it we'll close the question as it's not helpful to anyone else. I hope you will provide more as there were some interesting points in the question that I'm still curious about. – Aaron left Stack Overflow Sep 03 '17 at 13:47