我正在使用var chunks = [[5, 3, 1], [9, 2, 5], [2, 3, 7]];
// `x`: Index at which to start changing array
// `y`: An integer indicating the number of old array elements to remove
function shift(arr, x, y, replacement) {
arr.forEach(function(curr, index) {
// The elements to add to the array, beginning at the start index.
// start index: `x`
curr.splice(x, y, replacement)
});
return arr
}
// e.g.,
shift(chunks, -1, 1, null);
console.log(chunks);
来提高速度和简单性以导入Excel文件。
不幸的是,目前还没有能够排除数据集中不需要的选定列;为节省工作量,我使用read_excel
参数命名此类列"x"
,这比尝试跟踪col_names
,x1
等更容易。
然后我想尽可能地排除这些列,以避免额外的复制步骤,所以在伪代码中:
x2
我们可以使用read_excel("data.xlsx", col_names = c("x", "keep", "x"))[ , !"x"]
包中包含的示例数据集进行说明:
readxl
我看到的方法工作并不完全正常,例如存储library(readxl)
DF <- read_excel(system.file("extdata/datasets.xlsx", package = "readxl"),
col_names = c("x", "x", "length", "width", "x"), skip = 1L)
,我们现在可以这样做:
DF
这有效但需要通过存储它来复制DF <- DF[ , -grep("^x$", names(DF))]
,然后覆盖它;我希望删除与DF
相同的命令中的列,以正确地分配read_excel
ab initio 。
其他类似方法需要声明临时变量,如果可能,我希望避免使用,例如,
DF
有没有办法在没有创建不必要的临时变量的情况下解锁这些列?
(我可以转换为col_names <- c("x", "x", "length", "width", "x")
DF <- read_excel(system.file("extdata/datasets.xlsx", package = "readxl"),
col_names = col_names, skip = 1L)[ , -grep("^x$", col_names)]
,但我想知道是否有办法在没有data.table
的情况下这样做
答案 0 :(得分:2)
我没有看到避免复制的简单方法。但是使用管道可以实现一个衬管,不需要临时变量。 E.g:
library(magrittr)
read_excel(system.file("extdata/datasets.xlsx", package = "readxl"),
col_names = c("x", "x", "length", "width", "x"), skip = 1L) %>%
extract(, -grep("^x$", names(.))) ->
DF
答案 1 :(得分:2)
实际上有一种方法可以在readxl::read_excel
中执行此操作,虽然它有点隐藏,但我不知道列是否会暂时读入内存[无论如何]。诀窍是指定列类型,为那些你不想要的人输入"blank"
:
readxl::read_excel(system.file("extdata/datasets.xlsx", package = "readxl"),
col_types = c('blank', 'blank', 'numeric', 'numeric', 'text'))
## # A tibble: 150 x 3
## Petal.Length Petal.Width Species
## <dbl> <dbl> <chr>
## 1 1.4 0.2 setosa
## 2 1.4 0.2 setosa
## 3 1.3 0.2 setosa
## 4 1.5 0.2 setosa
## 5 1.4 0.2 setosa
## 6 1.7 0.4 setosa
## 7 1.4 0.3 setosa
## 8 1.5 0.2 setosa
## 9 1.4 0.2 setosa
## 10 1.5 0.1 setosa
## # ... with 140 more rows
需要注意的是,您需要知道所需列的所有数据类型,但我想您可以始终只使用文本开头并稍后使用type.convert
或其他内容进行清理。