2017-05-03 84 views
0

我有下面的數據,我需要管理:Organzing /清潔數據幀

data <- data.frame(Name=c("11C","11C","11C","11C","11C","20D","20D"), 
       PID=c("AD15E","AD15E","AD15E","AA05D","AA05D","Z48J","Z48J"), 
       Type=c("Home","Auto","Auto","Home","Auto","Auto","Home"), 
       Brand=c("A","B","C","H","I","P","D"), 
       Model=c("A152","K235","W54","H2","A57","Z23","Y0878")) 

通過獨特的名稱和PID,我想將數據從行轉換成列。 PID「AA05D」有兩個類型「自動」,所以我想第二行被轉換成它自己的列。

我不知道我可以用什麼來實現這一點。

我在尋找清潔的數據如下所示:

result <- data.frame(Name=c("11C","11C","20D"), 
       PID=c("AD15E","AA05D","Z48J"), 
       Home.Brand=c("A","H","D"), 
       Home.Model=c("A152","H2","Y0878"), 
       Auto1.Brand=c("B","I","P"), 
       Auto1.Model=c("K235","A57","Z23"), 
       Auto2.Brand=c("C","",""), 
       Auto2.Model=c("W54","","")) 

回答

0

有關使用data.tabledcast如何?

library(data.table) 

data$count <- ave(1:nrow(data), data$Name, data$PID, data$Type, FUN = function(x) 1:length(x)) 
dcast(setDT(data), Name + PID ~ Type + count, value.var = c("Brand", "Model")) 
# Name PID Brand_Auto_1 Brand_Auto_2 Brand_Home_1 Model_Auto_1 Model_Auto_2 Model_Home_1 
#1: 11C AA05D   I   NA   H   A57   NA   H2 
#2: 11C AD15E   B   C   A   K235   W54   A152 
#3: 20D Z48J   P   NA   D   Z23   NA  Y0878