Eitan
Eitan

Reputation: 13

Convert Range of Values to Names

I have data in the following format.

ID LoanAmtBin
1  20000
2  120000 
3  90000
4  50000
5  50000

I would like to change the values within a range to a name. For example, if a Loan is between 0 and 10,000 it should have the name 10K, if its between 10,001 and 25,000 it should have the 25K name etc.

Desired table:

ID LoanAmtBin
1  25K
2  250K 
3  100K
4  50K
5  50K

I've already converted the LoanAmtBin column to numeric. Then I'm using the following line of code to convert each value to a name.

LAmtCut <- cut(cleancc$LoanAmtBin, breaks = c(0, 10000, 25000, 50000, 100000, 250000, 500000, 1000000),
           labels = c("10K", "25K", "50K", "100K", "250K", "500K", "1Mil"))

When I run the LAmtCut, it shows the labels I want in the console. However, the column in my data frame is not changing. How do I get the values in the data frame to change?

Upvotes: 0

Views: 751

Answers (1)

JJEO
JJEO

Reputation: 31

The reason your data frame (DF) isn't changing is because the cut function outputs a vector, but you're not assigning that output back to a column in your DF. If you want to update your DF, then you need to assign the result back to a column. You can use the $ operator to do that.

Example:

test <- data.frame(ID = c(10000, 20000, 20000, 30000, 30000, 40000))

# outputs a factor vector
labs <- cut(test$ID, breaks = c(0, 10000, 20000, 30000, 40000, 50000), 
    labels = c("10k", "20k", "30k", "40k", "50k"))

# updates the data frame
test$ID <- labs

Note that ID is a factor variable rather than a character variable as well, which is something to keep in mind as you continue your analysis because they do differ in how they can be operated on.

Upvotes: 1

Related Questions