Split data.frame given one leveled column

Share:

Description

Split data.frame given one leveled column

Usage

1
dfsplit(x, which.col, levels)

Arguments

x

data.frame

which.col

column index or name

levels

character see base::factor

Value

named list

Author(s)

Xiaobei Zhao

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
x <- read.table(textConnection("
chr1  0   100
chr2  100 200
chr10 200 300
"),col.names=c('chr','start','end'))

## compare the results by base::split and dfsplit
split(x,f=x[,'chr'])
## $chr1
##    chr start end
## 1 chr1     0 100

## $chr10
##     chr start end
## 3 chr10   200 300

## $chr2
##    chr start end
## 2 chr2   100 200

dfsplit(x,'chr',c('chr1','chr2','chr10'))
## $chr1
##    chr start end
## 1 chr1     0 100

## $chr2
##    chr start end
## 2 chr2   100 200

## $chr10
##     chr start end
## 3 chr10   200 300

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.