English 中文(简体)
第3和第4行各移至某一行数之间的数据组
原标题:Removing every 3rd and 4th row in a dataframe between certain row numbers
  • 时间:2024-02-15 22:26:44
  •  标签:
  • r
  • dataframe

我有很长的数据框架,可以看上去(尽管该栏的编号有日期):

    df <- data.frame(Type = c(rep("date", times = 100)),
             Day = c(1:100))

在我的耳光中,有一段时期,我想从50到70年,删除每4个牢房中的第三和第四个。

  df <- data.frame(Type = c(rep("date", times = 80)),
             Date = c(1:52,55,56,59,60,63,64,67,68,71:90))

如果不把我想要删除的每个增长指数人工点名,是否有这样做的方法? 任何帮助都会受到非常赞赏!

最佳回答

模块操作员可对此类事情有用。

idx <- 1:1000
idx <- idx[idx < 51 | (idx + 1) %% 4 < 2 | idx > 70]
df <- df[idx, ]

或者,使用消极指数:

idx <- 51:70
idx <- idx[(idx + 1) %% 4 > 1]
df <- df[-idx, ]

根据这两种方法:

#> df[45:75, ]
   Type Day
45 date  45
46 date  46
47 date  47
48 date  48
49 date  49
50 date  50
51 date  51
52 date  52
55 date  55
56 date  56
59 date  59
60 date  60
63 date  63
64 date  64
67 date  67
68 date  68
71 date  71
72 date  72
73 date  73
74 date  74
75 date  75
76 date  76
77 date  77
78 date  78
79 date  79
80 date  80
81 date  81
82 date  82
83 date  83
84 date  84
85 date  85
问题回答

虽然我更喜欢“@zephryl”的解决办法,但这里是另一种选择,即,如果你要用<条码>删除<条码>/代码>关于<条码>的争论:

df[-unlist(lapply(3:4, (x) seq(50, 70, x)[-1])),]




相关问题
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 ...

热门标签