使用以下数据:
current-group()
使用mutate,我想在第一个唯一PID出现的行上的数据帧(HomeBrand,HomeModel,AutoBrand,AutoModel)中创建四个新列。
结果应如下所示:
data <- data.frame(Name=c("11C","11C","12C","12C","20D","20D"),
PID=c("AD15E","AD15E","AA05D","AA05D","Z48J","Z48J"),
Type=c("Home","Auto","Home","Auto","Home","Auto"),
Brand=c("A","B","C","H","I","D"),
Model=c("A152","K235","W54","H2","A57","Y0878"))
我尝试过使用mutate,但似乎无法弄清楚
答案 0 :(得分:3)
在结果中包含 Home 的 Type 列是没有意义的,因为 Type 列转到标题,对于每行,您混合了 Home 和 Auto 值;没有该列,它只是public class Employee {
public int Employee Id { get; set; }
public int ManagerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[ForeignKey("ManagerId")]
public virtual Employee Manager { get; set; }
}
的简单任务:
reshape
为了比较,这是你的结果:
reshape(data, idvar = c("Name", "PID"), timevar = "Type", direction = "wide")
# Name PID Brand.Home Model.Home Brand.Auto Model.Auto
#1 11C AD15E A A152 B K235
#3 12C AA05D C W54 H H2
#5 20D Z48J I A57 D Y0878
答案 1 :(得分:0)
我们可以使用dcast
library(data.table)
dcast(setDT(data), Name + PID ~ Type, value.var = c("Brand", "Model"), sep="")
# Name PID BrandAuto BrandHome ModelAuto ModelHome
#1: 11C AD15E B A K235 A152
#2: 12C AA05D H C H2 W54
#3: 20D Z48J D I Y0878 A57