-2

There is column (id 1) and the corresponding number of json file in (json) column. There is still the column (id2). How do I compare the values of id1 and id2 so that the names of json are sorted accordingly by id2. I tried so much.

Acc = read.xlsx ("path to file")
Df.new <- acc [order (acc $ id2),]

But it's just ascending ordering. I.E i just need to compare it. For example, find the value 2 in (id2) column , and then find value 2 in id1 and opposite value 2 of (id2) column, put the name of the json file, which in the (Json) column, that is corresponds to value 2 of (id1) column. How do that?

for example value 2 of (id1) column located at A3 cell of excel and value 2 of (id2) column located at D1283 so as output i need this and see the screen https://i.stack.imgur.com/rya2M.jpg

id1 json        id2 
1282    2309058401.json     2   0104010074.json

acc

H.Siw
  • 15
  • 5
  • 1
    Your description and the image are incomplete: `A3` is not visible, and it is unclear what you intend. Furthermore, you start with `order` and wanting to arrange things, but arranging has nothing to do with what appears to be a `merge` or join. I think you need to make this more of a [reproducible question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing *sample* data input and output. – r2evans Jul 31 '17 at 17:01
  • exactly , i need merge! – H.Siw Jul 31 '17 at 17:02
  • 3
    Perhaps something like `merge(acc, newdf, by.x="id1", by.y="id2", all.x=FALSE, all.y=TRUE)`. Caveat emptor: without usable sample data, this is untested. (BTW: please post code *you actually use*. The fact that you have `Acc` in line 1 and `acc` (case change) in line 2 is an indication that either (a) this is just code you typed here, not in your code; or (b) further bugs you have in your own code.) – r2evans Jul 31 '17 at 17:07

1 Answers1

1

This is a formalization of my comment, tested with sample data:

acc <- data.frame(id1 = 1:4,
                  txt = c("aaa", "bbb", "ccc", "ddd"),
                  stringsAsFactors = FALSE)
newdf <- data.frame(id2 = c(2, 4))

merge(acc, newdf, by.x = "id1", by.y = "id2", all.x = FALSE, all.y = TRUE)
#   id1 txt
# 1   2 bbb
# 2   4 ddd

Note that the first index (id1) is preserved, not id2.

r2evans
  • 141,215
  • 6
  • 77
  • 149