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.
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
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
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With