数据尝试
colnames(data) <- unlist(data[1,])
data <- data[-1,]
> data
# id user_id foreign_model_id machine_id
#2 1 3145 4 12
#3 2 4079 1 8
#4 3 1174 7 1
#5 4 2386 9 9
#6 5 5524 1 7
如果你愿意,你可以在第一行的删除后添加rownames(data) <- NULL
以纠正行号。
该操作后,可以选择喜欢
subset(data, data$machine_id==1)
# id user_id foreign_model_id machine_id
#4 3 1174 7 1
在碱R
对应于特定条件的行,该函数filter()
在OP建议是stats
命名空间的一部分,并且通常保留给分析的时间序列。
数据
data <- structure(list(C0 = structure(c(6L, 1L, 2L, 3L, 4L, 5L),
.Label = c("1", "2", "3", "4", "5", "id"), class = "factor"),
C1 = structure(c(6L, 3L, 4L, 1L, 2L, 5L), .Label = c("1174", "2386",
"3145", "4079", "5524", "user_id"), class = "factor"),
C2 = structure(c(5L, 2L, 1L, 3L, 4L, 1L),
.Label = c("1", "4", "7", "9", "foreign_model_id"), class = "factor"),
C3 = structure(c(6L, 2L, 4L, 1L, 5L, 3L),
.Label = c("1", "12", "7", "8", "9", "machine_id"), class = "factor")),
.Names = c("C0", "C1", "C2", "C3"), class = "data.frame",
row.names = c("1", "2", "3", "4", "5", "6"))
我想你已经读了错误的数据帧。您想要删除数据框的列名称并将行1作为新的列名称,对吗? –
是的,这是正确的。 –
然后你不能这样做:'colnames(data)= data [1,]'和'data = data [-1,]'? – Konrad