Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

r - Testing which is greater but value changes

I have a dataset df which I would like to test at the indicator, how many of the value is greater than the testing threshold. For example for A, I want to test how many of it exceeds threshold value1 of -1, how many exceeds threshold value2 of -0.5, threshold value3 of 1 and threshold value4 of 2. The thing is my testing threshold is different for each A,B and C. How should I loop it?

   df >
    A     B       C     
    1     5       2     
    2    12       3     
    3    -1       4    
    4     0       6    
    5    -9     -13    
    6    0.1      2     
    7    1        3     

Testing threshold

 ID    value1 value2 value3 value4
 A         -1   -0.5    1      2  
 B          1    6      9     12  
 C          3    3.2    3.4    3.5

Desired Output for A

ID    value1 value2 value3 value4
A        7       7       6      5 
question from:https://stackoverflow.com/questions/65895819/testing-which-is-greater-but-value-changes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming testing threshold is stored in dataframe called test, you could split it row-wise and use Map to find output for each ID how many values are greater than each threshold.

Map(function(x, y) colSums(sapply(y, `<`, x)), df, asplit(test[-1], 1))

#$A
#value1 value2 value3 value4 
#     7      7      6      5 

#$B
#value1 value2 value3 value4 
#     2      1      1      0 

#$C
#value1 value2 value3 value4 
#     2      2      2      2 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...