Reputation: 383
How can I create a column in a dataset conditioned to other two columns according to this example.
On this example I am showing all possible combinations from my data:
library(data.table)
data <- "chr start tag depth BCV
chr1 3273 chr1-3273 0 0
chr1 3274 chr1-3274 1 1
chr1 3275 chr1-3275 1 2
chr1 3276 chr1-3276 1 3
chr1 3277 chr1-3277 2 1
chr1 3278 chr1-3278 2 2
chr1 3279 chr1-3279 2 3
chr1 3280 chr1-3280 3 1
chr1 3281 chr1-3281 3 2
chr1 3282 chr1-3282 3 3"
data <- read.table(text=data, header=T)
Expected outcome:
newdata <- "chr start tag depth BCV states
chr1 3273 chr1-3273 0 0 0
chr1 3274 chr1-3274 1 1 1
chr1 3275 chr1-3275 1 2 2
chr1 3276 chr1-3276 1 3 3
chr1 3277 chr1-3277 2 1 4
chr1 3278 chr1-3278 2 2 5
chr1 3279 chr1-3279 2 3 6
chr1 3280 chr1-3280 3 1 7
chr1 3281 chr1-3281 3 2 8
chr1 3282 chr1-3282 3 3 9"
newdata <- read.table(text=newdata, header=T)
The requested column in the data.table (states) ranges from 0 to 9 depending on the combination of the other two columns.
Upvotes: 0
Views: 64
Reputation: 12559
this gives you the desired table:
setDT(data)
data[, states:=.I-1]
data
To ensure the order of your records with respect to the two columns depth
and BCV
you can set keys in the datatable before the data[, states:=.I-1]
.
Upvotes: 0
Reputation: 145755
In base R,
data$states = as.numeric(factor(paste(data$depth, data$BCV))) - 1
Upvotes: 1
Reputation: 49448
OP is not well phrased. I'm guessing your looking for:
setDT(data)[, states := .GRP - 1, by = .(depth, BCV)]
Upvotes: 3