Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hold range in data.frame (or table)

Tags:

range

r

I would like to do this

set.seed(667) 
df <- data.frame(a = sample(c(c(4,7),11,NA),  10, rep = TRUE), 
                 b = sample(c(1, 2, 3, NA, 5, 6),  10, rep=TRUE), 
                 c = sample(c(11, 12, 13, 14, 15, 16),  10, rep=TRUE))

but instead of getting this,

df
    a  b  c
1   4 NA 12
2   7  6 12
3  NA NA 14
4  11  1 16
5  NA  2 14
6  NA  3 13
7  11 NA 13
8  NA  6 15
9   7  3 16
10  7  5 16

I would like to get something this where I have a range at some points,

    a  b  c
1  4-7 NA 12
2  4-7  6 12
3  NA  NA 14
4  11   1 16
5  NA   2 14
6  NA   3 13
7  11  NA 13
8  NA   6 15
9  4-7  3 16
10 4-7  5 16

I'm confused and tired and asking for help.

Update after reading SimonO101's comments at 2013-09-09 22:30:14Z

I think my question could also be stated like this, I would like this data frame

data.frame(A = c(4:7, 9),B = c(1,2))

to show up like

  A   B
1 4:7 9
2   2 2
like image 216
Eric Fail Avatar asked Oct 04 '22 00:10

Eric Fail


2 Answers

Maybe you want this?

library(data.table)

d = data.table(A = list(c(4,7), 9),B = c(1,2))
#     A B
#1: 4,7 1
#2:   9 2

One more possibility is to store the unevaluated expression (it's really not clear what OP wants, so I'm just shooting in the dark here):

d = data.table(A = list(quote(4:7), 9), B = c(1,2))
#        A B
#1: <call> 1
#2:      9 2
d[,A]
#[[1]]
#4:7
#
#[[2]]
#[1] 9
lapply(d[, A], eval)
#[[1]]
#[1] 4 5 6 7
#
#[[2]]
#[1] 9
like image 109
eddi Avatar answered Oct 11 '22 15:10

eddi


You could use cut to convert the values to whatever intervals you like, and also set appropriate labels for each of the intervals like so:

newdf <- sapply( df , cut , breaks = c(1:4,7.01,8:16) , labels = c(1:3,"4-7",8:16) , right = TRUE )
#      a     b     c   
# [1,] "3"   NA    "12"
# [2,] "4-7" "4-7" "12"
# [3,] NA    NA    "14"
# [4,] "11"  NA    "16"
# [5,] NA    "1"   "14"
# [6,] NA    "2"   "13"
# [7,] "11"  NA    "13"
# [8,] NA    "4-7" "15"
# [9,] "4-7" "2"   "16"
#[10,] "4-7" "4-7" "16"
like image 35
Simon O'Hanlon Avatar answered Oct 11 '22 14:10

Simon O'Hanlon