我想将数据的子集设置为值。
在下面的例子中,我想为子集设置“carb”为4。
如何直接执行此操作 - 换句话说,如何将子集的查询与carb:= 4分配相结合?生成的数据集应该是完整的mtcars,但子集的碳水化合物设置为4。
library(data.table)
data(mtcars)
setDT(mtcars)
mtcars[, .SD[1], by = cyl]
# the subset:
cyl mpg disp hp drat wt qsec vs am gear carb
1: 6 21.0 160 110 3.90 2.62 16.46 0 1 4 4
2: 4 22.8 108 93 3.85 2.32 18.61 1 1 4 1
3: 8 18.7 360 175 3.15 3.44 17.02 0 0 3 2
# required for the subset :
cyl mpg disp hp drat wt qsec vs am gear carb
1: 6 21.0 160 110 3.90 2.62 16.46 0 1 4 4
2: 4 22.8 108 93 3.85 2.32 18.61 1 1 4 4
3: 8 18.7 360 175 3.15 3.44 17.02 0 0 3 4
答案 0 :(得分:3)
我猜这有效:
data(mtcars)
setDT(mtcars)[mtcars[,.I[1], by=cyl]$V1, carb:=4] #based on @Roland's comments
mtcars[,.SD[1], by=cyl]
# cyl mpg disp hp drat wt qsec vs am gear carb
#1: 4 22.8 108 93 3.85 2.32 18.61 1 1 4 4
#2: 6 21.0 160 110 3.90 2.62 16.46 0 1 4 4
#3: 8 18.7 360 175 3.15 3.44 17.02 0 0 3 4
dim(mtcars)
#[1] 32 11
拆分上述代码
DT <- data.table(mtcars)
DT[, .I[1], by=cyl] #V1 gives the numeric index of the rows for the first occurence of each levels of `cyl`
# cyl V1
#1: 6 1
#2: 4 3
#3: 8 5
DT[, .I[1], by=cyl]$V1
#[1] 1 3 5
DT[DT[, .I[1], by=cyl]$V1, carb:=4] #assign carb to 4 for those rows
DT[,.SD[1], by=cyl]
# cyl mpg disp hp drat wt qsec vs am gear carb
#1: 6 21.0 160 110 3.90 2.62 16.46 0 1 4 4
#2: 4 22.8 108 93 3.85 2.32 18.61 1 1 4 4
#3: 8 18.7 360 175 3.15 3.44 17.02 0 0 3 4
答案 1 :(得分:1)
尝试:
> mtcars[, .SD[1], by = cyl][,carb:=4,][]
cyl mpg disp hp drat wt qsec vs am gear carb
1: 6 21.0 160 110 3.90 2.62 16.46 0 1 4 4
2: 4 22.8 108 93 3.85 2.32 18.61 1 1 4 4
3: 8 18.7 360 175 3.15 3.44 17.02 0 0 3 4
或:
> mtcars[order(cyl)][!duplicated(cyl)]$carb = 4
> mtcars[order(cyl)][!duplicated(cyl)]$carb
[1] 4 4 4
> mtcars[order(cyl)][!duplicated(cyl)]
mpg cyl disp hp drat wt qsec vs am gear carb
1: 22.8 4 108 93 3.85 2.32 18.61 1 1 4 4
2: 21.0 6 160 110 3.90 2.62 16.46 0 1 4 4
3: 18.7 8 360 175 3.15 3.44 17.02 0 0 3 4
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
1: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
2: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
3: 22.8 4 108 93 3.85 2.320 18.61 1 1 4 4
4: 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
5: 18.7 8 360 175 3.15 3.440 17.02 0 0 3 4
6: 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1