mardi 30 avril 2019

How to extract the same and highest value?

I have a table like below, I like to extract rows if they have the same value in the rows 4 and 8 otherwise the maximum value.

Input: data

1   9708  10948  1  1   9708  10948  1
1  11590  18647  4  1  12897  15040  2
1  11590  18647  4  1  15040  15500  3
1  11590  18647  4  1  15950  16580  2
1 108570 109500  1  1 108570 109500  1
1 440900 443400  2  1 440900 441080  1
1 440900 443400  2  1 443140 443400  1
1 539670 542200  3  1 539700 540450  2
1 539670 542200  3  1 541070 541770  2
1 539670 542200  3  1 540450 541070  3
1 712695 715758 14  1 712799 712900  5
1 712695 715758 14  1 713010 713230  8
1 751500 759199  8  1 752555 752773  5
1 761083 764000  9  1 761083 761198  1
1 761083 764000  9  1 762200 762300  8
1 761083 764000  9  1 762300 762800  9



ideal Output:

1   9708  10948  1  1   9708  10948  1
1  11590  18647  4  1  15040  15500  3
1 108570 109500  1  1 108570 109500  1
1 440900 443400  2  1 440900 441080  1
1 440900 443400  2  1 443140 443400  1
1 539670 542200  3  1 540450 541070  3
1 712695 715758 14  1 713010 713230  8
1 751500 759199  8  1 752555 752773  5
1 761083 764000  9  1 762300 762800  9

I have tried two commands, the first one gives me the same value between columns 4 and 8, but I want something like the second one and ideal Output.

data<-data[which(data$V4 == data$V8),]

data<- data[ifelse(data$V4 == data$V8,data$V4,max(data$V8)),]

Aucun commentaire:

Enregistrer un commentaire