Add column which contains binned values of an integer column

后端 未结 3 561
时光说笑
时光说笑 2020-11-22 04:32

I have a dataframe with a few columns, one of those columns is ranks, an integer between 1 and 20. I want to create another column that contains a bin value like \"1-4\", \

3条回答
  •  有刺的猬
    2020-11-22 04:59

    dat <- "rank,name,info
    1,steve,red
    3,joe,blue
    6,john,green
    3,liz,yellow
    15,jon,pink"
    
    x <- read.table(textConnection(dat), header=TRUE, sep=",", stringsAsFactors=FALSE)
    x$bins <- cut(x$rank, breaks=seq(0, 20, 5), labels=c("1-5", "6-10", "11-15", "16-20"))
    x
    
      rank  name   info  bins
    1    1 steve    red   1-5
    2    3   joe   blue   1-5
    3    6  john  green  6-10
    4    3   liz yellow   1-5
    5   15   jon   pink 11-15
    

提交回复
热议问题