English 中文(简体)
删除每个系数水平最后两行
原标题:Removing the last 2 rows of each factor level in r

I have a dataframe, where a factor has different amounts of rows. I would like to remove the last 2 rows of each factor level. test<-data.frame(id=c(1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,3), val=c(3,5,4,6,7,4,1,6,7,8,4,2,0,3,6,8,1,2), trt=c(1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2))

其结果应如此:

   id val trt
1   1   3   1
2   1   5   1
3   1   4   1
4   2   4   1
5   2   1   1
6   2   6   1
7   2   7   1
8   3   2   2
9   3   0   2
10  3   3   2
11  3   6   2
12  3   8   2

我需要增加的一点是把其他各栏放在数据框架内。 我编辑了这个例子,以包括另一个与这项工作无关的因素,但我确实需要这个因素(以及一个更强大的团队)来进行今后的计算。

问题回答

<sequences alonid by id <>>>> /em>,应缩短到length - 1. 我们可为此使用avesubset

> subset(test, ave(id, id, FUN=(x) seq_along(x) < length(x) - 1L) == 1L)
   id val
1   1   3
2   1   5
3   1   4
6   2   4
7   2   1
8   2   6
9   2   7
12  3   2
13  3   0
14  3   3
15  3   6
16  3   8

Data:

> dput(test)
structure(list(id = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 
3, 3, 3, 3, 3), val = c(3, 5, 4, 6, 7, 4, 1, 6, 7, 8, 4, 2, 0, 
3, 6, 8, 1, 2)), class = "data.frame", row.names = c(NA, -18L
))

缩略语

library(data.table)

setDT(test)[,.(val = val[1:(.N - 2)]), id][]
#>     id val
#>  1:  1   3
#>  2:  1   5
#>  3:  1   4
#>  4:  2   4
#>  5:  2   1
#>  6:  2   6
#>  7:  2   7
#>  8:  3   2
#>  9:  3   0
#> 10:  3   3
#> 11:  3   6
#> 12:  3   8

或,使用<代码>。 SD

setDT(test)[,.SD[1:(.N - 2)], id][]
#>     id val trt
#>  1:  1   3   1
#>  2:  1   5   1
#>  3:  1   4   1
#>  4:  2   4   1
#>  5:  2   1   1
#>  6:  2   6   1
#>  7:  2   7   1
#>  8:  3   2   2
#>  9:  3   0   2
#> 10:  3   3   2
#> 11:  3   6   2
#> 12:  3   8   2

请注意,如果尚未按<代码>id分类,则将重新安排与原始数据有关的数据。

基础R(如果test,则按id分类):

test[sequence((n <- rle(test$id)[[1]]) - 2, c(1, cumsum(n[-length(n)]) + 1)),]
#>     id val trt
#>  1:  1   3   1
#>  2:  1   5   1
#>  3:  1   4   1
#>  4:  2   4   1
#>  5:  2   1   1
#>  6:  2   6   1
#>  7:  2   7   1
#>  8:  3   2   2
#>  9:  3   0   2
#> 10:  3   3   2
#> 11:  3   6   2
#> 12:  3   8   2

页: 1

> library(dplyr)

> test %>% slice_head(n = -2, by = id)
   id val
1   1   3
2   1   5
3   1   4
4   2   4
5   2   1
6   2   6
7   2   7
8   3   2
9   3   0
10  3   3
11  3   6
12  3   8




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签