-1

I have two columns of numbers. First column is called ddd and second column post. You can easily import my data into your Rstudio this way:

id <- "0B5V8AyEFBTmXM1VIYUYxSG5tSjQ"
Points <- read.csv(paste0("https://docs.google.com/uc?id=",id,"&export=download")) 

My question is how I can find out first, what is post when ddd is 0 AND second, if there is no 0 for post when ddd is 0, find the closest to 0? (so I need R to do the both checks for me?)

I have used the following R code which doesn't work:

Points$post[Points$ddd == 0]
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
rnorouzian
  • 7,397
  • 5
  • 27
  • 72
  • I get error `InternetOpenUrl failed: 'A connection with the server could not be established'` – akrun Feb 27 '17 at 06:46
  • @akrun, let me check please. – rnorouzian Feb 27 '17 at 06:48
  • Please post a small reproducible example using `dput` i.e. `dput(head(Points, 10))` – akrun Feb 27 '17 at 06:48
  • There is no `ddd` with value 0 in the example. Do you want to check with `post` ? – Ronak Shah Feb 27 '17 at 06:49
  • @RonakShah, yes these are from a function there may or may not be. So that's what I'm asking I need R to first check if there is a ddd with value "0" and if not then it gives me the closest to "0"? – rnorouzian Feb 27 '17 at 06:51
  • @akrun, I guess that probably has been an internet issue that you couldn't see the data, it is from a very complex 2000-line code function, but also try this: `id <- "0B5V8AyEFBTmXM1VIYUYxSG5tSjQ"; Points <- read.csv(sprintf("https://docs.google.com/uc?id=%s&export=download", id))` – rnorouzian Feb 27 '17 at 06:57
  • @parvinkarimi you have the case of when zero is not zero in R due to floating point numbers, see the output of `which((Points$ddd == 0) == T)` vs. `which((as.integer(Points$ddd) == 0) == T)`. See the section on the link titled "Don't test floating point numbers for exact equality." http://blog.revolutionanalytics.com/2009/03/when-is-a-zero-not-a-zero.html – Djork Feb 27 '17 at 07:01
  • `with(Points, post[abs(ddd) == min(abs(ddd))])` – Ronak Shah Feb 27 '17 at 07:04
  • I appreciate everyone's valuable input. – rnorouzian Feb 27 '17 at 07:10

1 Answers1

0

If you have a Points dataframe with two columns, post and ddd, zero or near zero could be acheived with which.min(abs(Points$ddd)) which will return the index so Points$post[which.min(abs(Points$ddd))] should get you there.

Note, you will have issues if you have multiple zeros or minimum values.

vincentmajor
  • 1,076
  • 12
  • 20