Home > Back-end >  Looking for R functionality to split a numerical column in a data frame into a categorical variable
Looking for R functionality to split a numerical column in a data frame into a categorical variable

Time:11-13

Problem
I'm trying to split a data column listing market cap for cryptocurrency projects into categorical data (in one column) using logical operators.

Solutions that I tried I'm using tidyverse, pipe operator and mutate to implement if-else statements with logical operators and trying to save as a categorical variable. Can't find the right answer on the web, but I'm most likely to noob a coder to understand the threads and workarounds I've been looking at.

library(httr)
library(jsonlite)
library(dplyr)
library(ggthemes)
library(ggplot2)
library(ggrepel)
library(googlesheets4)
library(tidyverse)

options(scipen=999)

# Get BTC API results from Nomics
NOMICS_API <- GET("https://api.nomics.com/v1/currencies/ticker?key=YOURKEY&interval=30d,365d&convert=EUR")

# Get json request results as text
NOMICS_API_TEXT <- content(NOMICS_API, "text")

# Make a useful object for R analysis
NOMICS_API_DF <- fromJSON(NOMICS_API_TEXT)


# Unnest nested tables 30d and 365d
DF_RAW <- unnest(NOMICS_API_DF, c("30d","365d"), names_repair = "universal")

# Clean, redefine and filter inactive projects
DF_CLEAN <- DF_RAW %>%
  filter(status == "active") %>%
  mutate(rank = as.integer(rank),
         price = as.numeric(price),
         num_pairs = as.integer(num_pairs), 
         num_exchanges = as.integer(num_exchanges),
         circulating_supply = as.numeric(circulating_supply),
         max_supply = as.numeric(max_supply),
         market_cap = as.numeric(market_cap)/(1*10^9),
         market_cap_dominance = as.numeric(market_cap_dominance)*100,
         high = as.numeric(high),
         high_timestamp = as.Date(high_timestamp),
         market_cap_tier = if(market_cap => 50) as.character("Big Cap")
           else if (50 > market_cap > 10)  as.character("Medium Cap")
           else if (10 > market_cap > 0)  as.character("Small Cap")
  )

Please, any pointers would be highly appreciated!

CodePudding user response:

Use case_when like below.
The pipe itself is just an example, the case_when does what the nested if/else in the question is trying to do.

DF_RAW %>%
  mutate(
    market_cap_tier = case_when(
      market_cap => 50 ~ "Big Cap",
      market_cap > 10 & market_cap < 50 ~ "Medium Cap",
      market_cap > 0 & market_cap < 10 ~ "Small Cap",
      TRUE ~ NA_character_
    )
  )
  • Related