數據嘗試
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