Robin_Hcp
Robin_Hcp

Reputation: 309

Replace the values of a data.frame using a given cluster table in R

I have a dataframe named 'databasis' in R having the following data

enter image description here

I am trying to create a new data.frame by assigning each percentage value of table 'databasis' a letter from the below cluster table.

enter image description here

For example, for company Aa in 200202 the letter assigned to the percentage would be equal to C.

enter image description here

Code for table 'databasis'

company <- c("Aa", "Bb", "Cc", "Dd") 
sector <- c("B", "A", "E", "Z") 
lastmarketcap <- c(18, 4571, 122, 239) 
X200202 <- c(0.0833, 0.0002, 0.00, 0.0085) 
X200203 <- c(0.2308, 0.00, 0.0083, 0.0042) 
X200204 <- c(0.125, 0.0007, 0.00, 0.0084) 


databasis <- data.frame(company, sector, lastmarketcap, X200202, X200203, X200204)

Thank you in advance for your help!

Upvotes: 0

Views: 58

Answers (1)

De Novo
De Novo

Reputation: 7600

Here we create a breaks vector corresponding to the max column in your image, labels corresponding to the grade column, and then cut each column into a factor with those labels.

breaks = c(-Inf,-0.05, 0, 0.05, 0.1, 0.15, 100)
labels = rev(LETTERS[1:6])
f <- function(col) cut(col, breaks = breaks, labels = labels)
new <- data.frame(databasis[1:3], lapply(databasis[-c(1:3)], f))
new
#   company sector lastmarketcap X200202 X200203 X200204
# 1      Aa      B            18       C       A       B
# 2      Bb      A          4571       D       E       D
# 3      Cc      E           122       E       D       E
# 4      Dd      Z           239       D       D       D

Upvotes: 1

Related Questions