Check if any value in an R vector is greater than or less than a certain value.

R ProgrammingServer Side ProgrammingProgramming

To check if any value in an R vector is greater than or less than a certain value, we can use any function.

For Example, if we have a vector called V and we want to check if any value in V is greater than 100 then we can use the command given below −

any(V>100)

Similarly, we can check if any value is less than 100 by using the command as follows −

any(V<100)

Check out the below Examples to understand how it works,

Example 1

Following snippet creates a sample data frame −

x1<-rpois(200,5)
x1

The following dataframe is created

[1] 4 6 5 4 5 4 8 5 4 7 6 3 1 8 7 1 4 4 6 9 3 8 3 6 4
[26] 6 2 7 3 1 3 12 3 4 2 4 1 3 6 6 5 5 5 3 3 10 4 5 2
4
[51] 4 5 5 8 5 2 3 5 3 6 10 7 4 3 4 2 10 5 4 6 4 7 5 4
2
[76] 5 6 6 7 3 5 3 5 2 5 5 4 6 3 6 12 4 6 3 4 3 6 9 3
2
[101] 4 7 4 7 9 6 8 7 5 5 2 3 9 5 4 4 5 8 7 5 5 3 5 4
4
[126] 6 7 3 5 3 5 5 10 5 9 4 3 7 9 6 2 6 10 7 4 7 6 5 2
5
[151] 7 2 0 6 6 3 9 5 3 6 5 8 2 3 9 7 6 7 3 5 4 6 6 4
8
[176] 11 7 6 7 3 1 1 4 5 4 4 2 9 5 6 5 5 0 6 3 8 6 3 3
6

Add the following code to the above snippet −

x1<-rpois(200,5)
any(x1>10)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Add the following code to the above snippet −

x1<-rpois(200,5)
any(x1>15)

If you execute the above given snippet, it generates the following Output −

[1] FALSE

Add the following code to the above snippet −

x1<-rpois(200,5)
any(x1<1)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Example 2

Following snippet creates a sample data frame −

x2<-rpois(200,50)
x2

The following dataframe is created

[1] 50 45 50 49 55 51 55 44 54 53 43 44 50 61 52 41 43 47 37 46 59 41 50 48
49
[26] 55 50 56 48 60 53 56 41 64 44 64 47 48 52 50 38 61 56 46 40 45 52 54 46
54
[51] 42 47 47 39 44 49 27 51 55 49 56 56 54 52 48 51 53 42 54 48 46 55 51 42
63
[76] 51 41 46 43 45 49 51 43 49 40 52 59 50 61 54 52 61 50 57 59 50 58 50 47
45
[101] 37 47 52 49 51 49 43 60 63 47 48 46 51 48 66 50 51 40 55 50 53 39 52 43
39
[126] 50 46 64 48 47 46 43 68 45 54 47 44 44 47 40 52 59 48 63 40 42 56 54 48
48
[151] 44 46 49 53 57 44 37 49 49 52 51 54 52 49 54 54 47 58 34 62 57 44 37 47
48
[176] 59 47 31 52 55 45 50 44 57 38 53 55 36 50 52 56 46 51 43 57 54 62 46 43
57

Add the following code to the above snippet −

x2<-rpois(200,50)
any(x2>70)

If you execute the above given snippet, it generates the following Output −

[1] FALSE

Add the following code to the above snippet −

x2<-rpois(200,50)
any(x2<30)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Add the following code to the above snippet −

x2<-rpois(200,50)
any(x2<20)

If you execute the above given snippet, it generates the following Output −

[1] FALSE

Example 3

Following snippet creates a sample data frame −

x3<-round(rnorm(200),0)
x3

The following dataframe is created

[1] 0 1 0 1 -2 -2 0 2 -3 -1 -1 -1 -1 1 0 1 -1 0 -2 -2 1 -2 -1 -1
0
[26] 0 1 0 0 0 -1 1 1 0 0 -1 -2 0 1 0 0 1 1 0 0 0 -1 0 -1 -
1
[51] 0 0 0 2 0 -1 0 0 -1 0 -2 0 -1 1 1 0 -1 1 0 0 0 0 0 0
0
[76] 1 -1 -1 0 1 0 0 0 1 1 0 0 1 -1 2 -1 0 2 -1 -1 0 1 1 -2 -
1
[101] -1 -1 0 -1 0 0 -1 -1 -1 1 -1 3 0 1 0 1 -1 2 0 -1 0 0 1 0
1
[126] -1 1 0 0 2 0 0 -2 0 -1 0 0 1 -1 0 0 0 1 -2 2 0 1 -1 0 -
1
[151] -2 0 0 -2 0 2 1 0 -3 -1 -2 2 2 1 -1 0 0 0 0 0 -1 0 0 -1
1
[176] 3 1 1 0 0 0 0 -2 1 0 0 0 0 1 -1 0 1 -1 -1 -1 0 1 -1 2 -
1

Add the following code to the above snippet −

x3<-round(rnorm(200),0)
any(x3>3)

If you execute the above given snippet, it generates the following Output −

[1] FALSE

Example 4

Following snippet creates a sample data frame −

x4<-rpois(200,100)
x4

The following dataframe is created

[1] 108 107 99 114 104 95 121 83 94 100 103 106 102 99 95 90 93 105
[19] 93 89 115 109 113 109 99 95 90 96 112 104 107 119 107 90 105 87
[37] 92 101 97 108 92 103 98 93 135 88 115 101 106 102 99 106 114 121
[55] 92 116 101 103 93 105 102 94 98 95 103 86 101 111 95 97 102 100
[73] 106 106 90 82 80 129 104 86 103 104 109 97 103 95 107 122 107 91
[91] 81 96 94 94 106 105 100 86 101 99 105 103 89 115 101 97 121 105
[109] 102 101 107 99 101 101 106 110 110 94 117 111 99 103 96 103 90 89
[127] 85 103 113 88 82 102 92 96 100 85 102 102 109 102 99 96 91 104
[145] 114 88 122 111 104 96 98 92 98 100 94 107 92 97 91 109 86 118
[163] 90 98 105 113 100 105 122 94 102 84 97 114 97 117 104 106 108 99
[181] 88 107 117 108 90 117 112 98 88 104 82 100 86 94 94 103 94 98
[199] 101 100

Add the following code to the above snippet −

x4<-rpois(200,100)
any(x4>125)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Add the following code to the above snippet −

x4<-rpois(200,100)
any(x4>130)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Add the following code to the above snippet −

x4<-rpois(200,100)
any(x4>135)

If you execute the above given snippet, it generates the following Output −

[1] FALSE

Add the following code to the above snippet −

x4<-rpois(200,100)
any(x4<95)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Add the following code to the above snippet −

x4<-rpois(200,100)
any(x4<85)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Add the following code to the above snippet −

x4<-rpois(200,100)
any(x4<75)

If you execute the above given snippet, it generates the following Output −

[1] FALSE

Example 5

Following snippet creates a sample data frame −

x5<-rpois(200,1000)
x5

The following dataframe is created

[1] 1021 1007 1010 968 1029 989 994 946 996 1011 1023 994 985 1005
1013
[16] 1021 930 975 998 984 1030 946 1018 1066 1000 1016 976 979 1023
992
[31] 1005 1004 970 992 978 991 1047 952 979 983 1003 1039 989 986
1026
[46] 986 1013 1017 990 983 953 970 1021 1037 993 986 981 982 1028
1076
[61] 990 1055 1030 998 1015 984 977 940 1067 1015 921 1028 1017 949
993
[76] 979 1024 1052 988 957 1034 949 991 1055 1075 962 985 1008 890
962
[91] 984 1030 967 977 957 968 992 981 958 1022 950 1026 1034 987
1036
[106] 1040 1030 1033 969 991 1022 964 1044 1015 979 960 1013 1039 982
988
[121] 964 985 1005 941 966 985 1009 1060 992 975 1026 980 994 1034
1019
[136] 963 1025 1032 989 1029 998 1033 1037 1068 1002 1031 1064 1025 973
1006
[151] 976 998 1014 999 960 1010 938 999 1015 1007 994 1007 967 1017
966
[166] 1030 967 1002 1036 946 997 973 937 1018 1008 1007 943 1002 1090
981
[181] 949 1028 1003 1056 966 966 1018 967 986 1052 931 1022 1014 970
1012
[196] 991 992 994 1003 992

Add the following code to the above snippet −

x5<-rpois(200,1000)
any(x5<950)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Add the following code to the above snippet −

x5<-rpois(200,1000)
any(x5<925)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Add the following code to the above snippet −

x5<-rpois(200,1000)
any(x5<900)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Add the following code to the above snippet −

x5<-rpois(200,1000)
any(x5<850)

If you execute the above given snippet, it generates the following Output −

[1] FALSE

Add the following code to the above snippet −

x5<-rpois(200,1000)
any(x5>1050)

If you execute the above given snippet, it generates the following Output −

[1] TRUE

Add the following code to the above snippet −

x5<-rpois(200,1000)
any(x5>1100)

If you execute the above given snippet, it generates the following Output −

[1] FALSE
raja
Updated on 02-Nov-2021 07:55:05

Advertisements