简体   繁体   English

如何在特定列中用NA替换第一个n1行和最后一行n2行

[英]How to replace first n1 and last n2 rows with NA in specific columns

Given the following example: 给出以下示例:

library(data.table)
mat <- data.table(x = c(1:10), y = c(11:20), z = c(21:30))

cut.head <- c(0, 2, 1) 
cut.tail <- c(3, 1, 2) 

cut.head represents the number of rows that each column will be NA from top. cut.head表示每列从顶部开始的行数。

cut.tail represents the number of rows that each column will be NA from last. cut.tail表示每列从最后一列开始的行数。

For example, if cut.head is used, 1st and 2nd rows of column y will be NAs, as well as the 1st column of z 例如,如果使用cut.head ,则列y的第1行和第2行将是NA,以及z的第1列

I would like the return as follows: 我希望返回如下:

     x  y  z
 1:  1 NA NA
 2:  2 NA 22
 3:  3 13 23
 4:  4 14 24
 5:  5 15 25
 6:  6 16 26
 7:  7 17 27
 8: NA 18 28
 9: NA 19 NA
10: NA NA NA

Thank you 谢谢

I'd just use a for loop with := (or set() ) so it's fast and (fairly) easy to read. 我只是使用带有:= (或set() )的for循环,因此它很快且(相当)易于阅读。

> for (i in 1:3) mat[seq_len(cut.head[i]), (i):=NA]
> mat
     x  y  z
 1:  1 NA NA
 2:  2 NA 22
 3:  3 13 23
 4:  4 14 24
 5:  5 15 25
 6:  6 16 26
 7:  7 17 27
 8:  8 18 28
 9:  9 19 29
10: 10 20 30

Notice that the LHS of := accepts column numbers as well as names. 请注意,LHS :=接受列号和名称。 As an aside, this is valid : 另外,这是有效的:

DT[, 2:=2]   # assign 2 to column 2

Wrapping the LHS of := with parenthesis, (i):=NA , tells it to use the variable's value rather than its name. 将LHS包含在:=括号中, (i):=NA ,告诉它使用变量的值而不是其名称。

For the tail I first tried the following but .N isn't available in i . 对于尾巴,我首先尝试了以下但是.Ni不可用。 I've added that as a feature request, FR#724 . 我已经添加了作为功能请求, FR#724
UPDATE : Now added to v1.9.3 on 11 Jul 2014 更新 :现已添加到2014年7月11日的v1.9.3

for (i in 1:3) mat[.N+1-seq_len(cut.tail[i]), (i):=NA]
# .N now works in i
> mat
     x  y  z
 1:  1 NA NA
 2:  2 NA 22
 3:  3 13 23
 4:  4 14 24
 5:  5 15 25
 6:  6 16 26
 7:  7 17 27
 8: NA 18 28
 9: NA 19 NA
10: NA NA NA
>

We no longer have to live with a repetition of the symbol mat : 我们不再需要重复使用符号mat

> for (i in 1:3) mat[nrow(mat)+1-seq_len(cut.tail[i]), (i):=NA]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R 用 NA 替换第一个 n 列值 - R replace first n column value with NA running.test() 仅适用于偶数 n1 和 n2 - runs.test() works only with even numbers of n1 and n2 在 R 中找到 vector1 的 n1 个元素和 vector2 的 n2 个元素的所有组合? - Find all combinations of n1 elements from vector1 and n2 elements from vector2 in R? 如何用R中的NA替换特定行和列中的某些值? - How to replace certain values in a specific rows and columns with NA in R? 找到包含在两个`n2` FALSE之间的`n1` TRUE,整个事物包含在`n3` TRUE之间,等等 - Finding `n1` TRUEs wrapped in between two `n2` FALSEs, the whole thing wrapped in between `n3` TRUEs, etc 如何使用参考表将特定行和列中的值替换为 NA? - How to replace values in specific rows and columns with NA using a reference table? 如果最近n次观察中没有任何NA,如何选择列? 如果相邻NA的观测值多于x,如何删除列? - How to select columns if there is not any NA in the last n observations? How to drop columns if there are more than x adjacent NA's observations? 如何使用 R 删除 dataframe 中的第一行和最后 N 行? - How to remove first and last N rows in a dataframe using R? 扔掉第一行和最后一行 - Throw away first and last n rows 在所有其他列中的行均为NA的条件下过滤一列的行,并重复n列 - Filter rows of one column on the condition that rows in all other columns are NA, and repeat for n columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM