2

I have data with the following format:

structure(list(cat = structure(c(1L, 2L, 3L, 1L, 2L, 2L, 3L, 
3L, 3L, 3L, 1L, 2L), .Label = c("A", "B", "C"), class = "factor"), 
ID = structure(c(1L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 2L, 
3L, 4L), .Label = c("s1", "s10", "s11", "s12", "s2", "s3", 
"s4", "s5", "s6", "s7", "s8", "s9"), class = "factor"), val = c(150, 
750, 950, 104, 726, 797, 890, 912, 994, 1004, 199, 704), 
LWR = c(100, 700, 900, NA, NA, NA, NA, NA, NA, NA, NA, NA
), UPP = c(200, 800, 1000, NA, NA, NA, NA, NA, NA, NA, NA, 
NA)), .Names = c("cat", "ID", "val", "LWR", "UPP"), row.names = c(NA, 
-12L), class = "data.frame")

Which looks like:

    cat ID  val LWR  UPP
1    A  s1  150 100  200
2    B  s2  750 700  800
3    C  s3  950 900 1000
4    A  s4  104  NA   NA
5    B  s5  726  NA   NA
6    B  s6  797  NA   NA
7    C  s7  890  NA   NA
8    C  s8  912  NA   NA
9    C  s9  994  NA   NA
10   C s10 1004  NA   NA
11   A s11  199  NA   NA
12   B s12  704  NA   NA

What I want to do is find a value in the val column having the same cat that lies closest to either the LWR or UPP values. It's probably easiest to understand by looking at the desired output:

  cat id val LWR  UPP  LS NLWR  US NUPP
1   A s1 150 100  200  s4  104 s11  199
2   B s2 750 700  800 s12  704  s6  797
3   C s3 950 900 1000  s8  912  s9  994

The new coloums (LS and NLWR/US and NUPP) are the same as the ids and vals from the extracted rows, just given new column names. I've tried to run this using various forms of "which" and then reforming the data but haven't had any luck. Is there a direct way to do this, or will it always take multiple steps?

4

1 に答える 1