How to Apply the cut() Function in R (Example Code)
In this article, I’ll illustrate how to convert numeric values to factorial ranges using the cut function in R.
Creation of Exemplifying Data
set.seed(983274) # Setting random seed values <- rnorm(100) # Creating random numeric vector values # Returning example vector to console # [1] 0.40150676 -0.76993138 0.23000627 -0.05045942 0.71099308 0.37033693 0.11330070 -0.41928509 # [9] 1.06894038 -0.62691475 -0.79267370 -0.27336681 -0.10845950 0.93664835 0.90094306 0.47078294 # [17] 0.30540556 0.54845500 -1.14409650 0.69744273 0.72418163 -1.53813778 -1.06202484 0.22428300 # [25] -0.16847998 -0.35091209 1.86813434 -1.38689926 0.51949421 1.36549000 0.72729700 0.24615412 # [33] -0.43263271 1.87313779 0.43564784 0.52563979 -0.96469066 0.86751379 -1.49740529 -1.56290204 # [41] 0.99401571 0.17104414 0.70500853 1.51846038 -1.78155345 0.12425432 1.35616836 -1.32629794 # [49] -0.73069126 0.92168637 0.94117219 -1.67988878 -0.92412273 0.54350214 -0.95276054 -0.23223560 # [57] 0.59072670 0.23910122 -0.32545594 -1.44210109 1.87283178 -0.40301202 -0.60520409 -1.33725034 # [65] -0.90886013 -0.83522615 1.65880589 0.38442999 -0.41010107 0.97708486 -0.06202119 -1.85715389 # [73] 1.48325005 0.60267946 -0.91117608 -0.98206665 1.52745092 0.27558336 0.03443715 2.22353920 # [81] -1.54413606 0.06912045 0.42826854 0.71107666 0.62833025 -1.54639682 0.79391355 -0.25585386 # [89] 0.50153503 1.72121360 -1.33965108 0.69150918 -0.38594831 -1.27289393 0.80040060 0.25639652 # [97] -1.18246234 0.48179087 -0.20169862 1.02057214 |
set.seed(983274) # Setting random seed values <- rnorm(100) # Creating random numeric vector values # Returning example vector to console # [1] 0.40150676 -0.76993138 0.23000627 -0.05045942 0.71099308 0.37033693 0.11330070 -0.41928509 # [9] 1.06894038 -0.62691475 -0.79267370 -0.27336681 -0.10845950 0.93664835 0.90094306 0.47078294 # [17] 0.30540556 0.54845500 -1.14409650 0.69744273 0.72418163 -1.53813778 -1.06202484 0.22428300 # [25] -0.16847998 -0.35091209 1.86813434 -1.38689926 0.51949421 1.36549000 0.72729700 0.24615412 # [33] -0.43263271 1.87313779 0.43564784 0.52563979 -0.96469066 0.86751379 -1.49740529 -1.56290204 # [41] 0.99401571 0.17104414 0.70500853 1.51846038 -1.78155345 0.12425432 1.35616836 -1.32629794 # [49] -0.73069126 0.92168637 0.94117219 -1.67988878 -0.92412273 0.54350214 -0.95276054 -0.23223560 # [57] 0.59072670 0.23910122 -0.32545594 -1.44210109 1.87283178 -0.40301202 -0.60520409 -1.33725034 # [65] -0.90886013 -0.83522615 1.65880589 0.38442999 -0.41010107 0.97708486 -0.06202119 -1.85715389 # [73] 1.48325005 0.60267946 -0.91117608 -0.98206665 1.52745092 0.27558336 0.03443715 2.22353920 # [81] -1.54413606 0.06912045 0.42826854 0.71107666 0.62833025 -1.54639682 0.79391355 -0.25585386 # [89] 0.50153503 1.72121360 -1.33965108 0.69150918 -0.38594831 -1.27289393 0.80040060 0.25639652 # [97] -1.18246234 0.48179087 -0.20169862 1.02057214
Example: Using cut() Function in R
values_cut <- cut(values, breaks = -5:5) # Applying cut() function values_cut # Returning output # [1] (0,1] (-1,0] (0,1] (-1,0] (0,1] (0,1] (0,1] (-1,0] (1,2] (-1,0] (-1,0] (-1,0] (-1,0] # [14] (0,1] (0,1] (0,1] (0,1] (0,1] (-2,-1] (0,1] (0,1] (-2,-1] (-2,-1] (0,1] (-1,0] (-1,0] # [27] (1,2] (-2,-1] (0,1] (1,2] (0,1] (0,1] (-1,0] (1,2] (0,1] (0,1] (-1,0] (0,1] (-2,-1] # [40] (-2,-1] (0,1] (0,1] (0,1] (1,2] (-2,-1] (0,1] (1,2] (-2,-1] (-1,0] (0,1] (0,1] (-2,-1] # [53] (-1,0] (0,1] (-1,0] (-1,0] (0,1] (0,1] (-1,0] (-2,-1] (1,2] (-1,0] (-1,0] (-2,-1] (-1,0] # [66] (-1,0] (1,2] (0,1] (-1,0] (0,1] (-1,0] (-2,-1] (1,2] (0,1] (-1,0] (-1,0] (1,2] (0,1] # [79] (0,1] (2,3] (-2,-1] (0,1] (0,1] (0,1] (0,1] (-2,-1] (0,1] (-1,0] (0,1] (1,2] (-2,-1] # [92] (0,1] (-1,0] (-2,-1] (0,1] (0,1] (-2,-1] (0,1] (-1,0] (1,2] # Levels: (-5,-4] (-4,-3] (-3,-2] (-2,-1] (-1,0] (0,1] (1,2] (2,3] (3,4] (4,5] |
values_cut <- cut(values, breaks = -5:5) # Applying cut() function values_cut # Returning output # [1] (0,1] (-1,0] (0,1] (-1,0] (0,1] (0,1] (0,1] (-1,0] (1,2] (-1,0] (-1,0] (-1,0] (-1,0] # [14] (0,1] (0,1] (0,1] (0,1] (0,1] (-2,-1] (0,1] (0,1] (-2,-1] (-2,-1] (0,1] (-1,0] (-1,0] # [27] (1,2] (-2,-1] (0,1] (1,2] (0,1] (0,1] (-1,0] (1,2] (0,1] (0,1] (-1,0] (0,1] (-2,-1] # [40] (-2,-1] (0,1] (0,1] (0,1] (1,2] (-2,-1] (0,1] (1,2] (-2,-1] (-1,0] (0,1] (0,1] (-2,-1] # [53] (-1,0] (0,1] (-1,0] (-1,0] (0,1] (0,1] (-1,0] (-2,-1] (1,2] (-1,0] (-1,0] (-2,-1] (-1,0] # [66] (-1,0] (1,2] (0,1] (-1,0] (0,1] (-1,0] (-2,-1] (1,2] (0,1] (-1,0] (-1,0] (1,2] (0,1] # [79] (0,1] (2,3] (-2,-1] (0,1] (0,1] (0,1] (0,1] (-2,-1] (0,1] (-1,0] (0,1] (1,2] (-2,-1] # [92] (0,1] (-1,0] (-2,-1] (0,1] (0,1] (-2,-1] (0,1] (-1,0] (1,2] # Levels: (-5,-4] (-4,-3] (-3,-2] (-2,-1] (-1,0] (0,1] (1,2] (2,3] (3,4] (4,5]