DJ-AFC DJ-AFC - 1 year ago 81
R Question

Find closest value from column and return number from adjacent column (within lists)

I have a large list (List1) of x data frames, each consisting of (let's say) 4 observations of 2 variables. A typical data frame looks like this:

1861 2.09
1862 2.17
1863 2.02
1864 2.04

I wish to find the value in the second column that is closest to the value 2 (but ideally not more than 2.05 and not less than 1.95) and then return the year from the adjacent column to the left. This is straightforward in Excel using functions such as VLOOKUP and INDEX, but is there any equivalent function in R? Ignoring the part where it needs to be >=1.95 and <=2.05, I have so far tried:

result=lapply(List1, function(x) which.min(abs(x-2)))

but this returns as error:

Error in which.min(abs(x - 2)) :
(list) object cannot be coerced to type 'double'

I don't think I am far away here, but can anyone please suggest where I can correct this?

Answer Source

Your function is apply to each element of your list, which are data.frames.

So, to make your statement work, you need to use the column of x on which you want to make the test:

result <- lapply(List1, function(x) x[which.min(abs(x$TEMP-2)), ])


result <- lapply(List1, function(x) x$YEAR[which.min(abs(x$TEMP-2))])

if you only want the year.

If you want to add the other conditions, try:

result <- lapply(List1, function(x) {x <- x[x$TEMP <= 2.05 & x$TEMP >= 1.95, ] ; return(x$YEAR[which.min(abs(x$TEMP-2))])})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download