我有一个来自机器输出的数据集,并且列以数字命名。我需要按名称删除某些列,因为我不想依赖于范围的位置(如42:67,在不同的数据集中可能是40:60)。当我读入CSV数据集时,我设置了check.names=FALSE
,以便在每列之前没有x
。我这样做是因为当我融合/收集数据时,我需要数字方面来排序和绘制数据,所以我不想处理x。
以下是我正在尝试的不起作用......
#Listing the column names to cut from beginning
beg.noise <- seq(from = 285, to = 414, by = 3)
#Listing the column names to cut from ending
end.blank <- seq(from = 1134, to = 1182, by = 3)
#Merging lists
columns.to.cut <- c(beg.noise, end.blank)
#Method 1
clean.data <- subset(sample.data, select= -columns.to.cut)
#Method 2
clean.data <-sample.data[,-columns.to.cut]
#Method 3 not much different that 1st
clean.data <- dplyr::select(sample.data, -columns.to.cut)
包含300列和2行观察的示例数据
sample.data <- as.data.frame(matrix(ncol=300, nrow=3, byrow = TRUE, c(as.character(seq(from=285, to= 1182, by=3)), rnorm(300, mean=0, sd=1), rnorm(300, mean=0, sd=1))))
#Setting first row as column headers
colnames(sample.data) <- as.character(unlist(sample.data[1,]))
sample.data = sample.data[-1, ]
答案 0 :(得分:1)
即使它们是数字,您的列名也属于character
类:
class(colnames(sample.data[1]))
[1] "character"
因此类numeric
的向量将不匹配,即使它们看起来相同。只需应用函数as.character
即可将它们从numeric
转换为character
:
beg.noise <- as.character(seq(from = 285, to = 414, by = 3))
答案 1 :(得分:1)
你说“当我融化/收集数据时,我需要数字方面来排序和绘制数据”。这表明了另一种选择:在列名中留下“X”并在 <{em>> gather
后处理。
例如 - 删除范围2:3
library(dplyr)
sample_data <- data.frame(X1 = 1:5,
X2 = 6:10,
X3 = 11:15,
X4 = 16:20)
sample_data %>%
gather(variable, value) %>%
# remove the X and convert to numeric
mutate(variable = gsub("X", "", variable),
variable = as.numeric(variable)) %>%
filter(!between(variable, 2, 3))
variable value
1 1 1
2 1 2
3 1 3
4 1 4
5 1 5
6 4 16
7 4 17
8 4 18
9 4 19
10 4 20