我有一个幻想团队的数据框架,我想以不同的方式查看它。
Position Name Salary GameInfo FPPG teamAbbrev TeamSalary TotalPoints
9 RB Devonta Freeman 8000 TB@Atl 01:00PM ET 28.443 Atl 49800 203.716
39 WR Alshon Jeffery 6400 Min@Chi 01:00PM ET 22.250 Chi 49800 203.716
45 WR Steve Smith Sr. 6100 SD@Bal 01:00PM ET 21.133 Bal 49800 203.716
47 QB Andy Dalton 6000 Cin@Pit 01:00PM ET 24.957 Cin 49800 203.716
55 RB Doug Martin 5500 TB@Atl 01:00PM ET 18.833 TB 49800 203.716
64 WR Martavis Bryant 5300 Cin@Pit 01:00PM ET 25.150 Pit 49800 203.716
126 TE Antonio Gates 4800 SD@Bal 01:00PM ET 24.350 SD 49800 203.716
128 WR Stefon Diggs 4800 Min@Chi 01:00PM ET 21.433 Min 49800 203.716
383 DST Broncos 2900 GB@Den 08:30PM ET 17.167 Den 49800 203.716
DST.391 DST Giants 2700 NYG@NO 01:00PM ET 9.714 NYG 49800 190.930
QB.4 QB Tom Brady 8300 Mia@NE 08:25PM ET 28.343 NE 49800 190.930
RB.137 RB Danny Woodhead 4500 SD@Bal 01:00PM ET 17.214 SD 49800 190.930
RB.55 RB Doug Martin 5500 TB@Atl 01:00PM ET 18.833 TB 49800 190.930
RB.9 RB Devonta Freeman 8000 TB@Atl 01:00PM ET 28.443 Atl 49800 190.930
TE.126 TE Antonio Gates 4800 SD@Bal 01:00PM ET 24.350 SD 49800 190.930
WR.64 WR Martavis Bryant 5300 Cin@Pit 01:00PM ET 25.150 Pit 49800 190.930
WR.39 WR Alshon Jeffery 6400 Min@Chi 01:00PM ET 22.250 Chi 49800 190.930
WR.145 WR Rishard Matthews 4300 Mia@NE 08:25PM ET 16.633 Mia 49800 190.930
我正在尝试转置并操纵它以使其打印,
team1 1 Devonta Freeman Alshon Jeffery Steve Smith Sr. Andy Dalton Doug Martin Martavis Bryant Antonio Gates Stefon Diggs Broncos 49800 203.716
team2 2 Giants Tom Brady Danny Woodhead Doug Martin Devonta Freeman Antonio Gates Martavis Bryant Alshon Jeffery Rishard Matthews 49800 190.930
所有这一切都在一行中。并删除其余信息。列名将相同,但我可以稍后添加。如果我不能
那么按位置订购会很好但不是很重要有关如何解决这个问题的想法吗?
由于
答案 0 :(得分:1)
好的,我已经更新了代码,在我的示例数据框中包含了一个索引列。在我的情况下,它被称为"索引"。为了通过索引循环,我创建了一个提取唯一级别的向量(ind = unique(data $ Index)),在这种情况下只有1和2,但在你的情况下是10,000级。然后在" j"循环它将按每个索引提取数据,并为每个团队提取所需的信息。
data = as.data.frame(list(Position = c("WR","WR","QB","RB","WR","RB","WR","TE","DST"),
Name=c("Julio Jones","DeAndre Hopkins","Tom Brady","Devonta Freeman",
"Alshon Jeffery","Chris Ivory","Martavis Bryant",
"Antonio Gates","Broncos"),
Salary = c(9200,8700,8300,8000,6400,6000,5300,4800,2900),
FPPG = c(25.143,25.657,28.343,28.443,22.250,20.300,
25.150,24.350,17.167),
TeamSalary = rep(59600,9),
TotalPoints = rep(216.803,9),
Index = rep(1,9)))
data1 = as.data.frame(list(Position = c("WR","WR","QB","RB","WR","RB","WR","TE","DST"),
Name=c(paste("Player",1:9,sep="")),
Salary = c(rnorm(9,5000,100)),
FPPG = c(rnorm(9,21,1)),
TeamSalary = rep(59600,9),
TotalPoints = rep(216.803,9),
Index = rep(2,9)))
data=rbind(data,data1)
data$Name = as.character(data$Name)
test2=NULL
ind = unique(data$Index)
for(j in 1:length(ind)){
data1 = data[data$Index == ind[j],]
points = 0
salary = 0
test1 = as.data.frame(matrix(nrow = 1,ncol=9))
names(test1) = c("QB", "RB", "RB", "WR", "WR", "WR", "TE", "FLEX", "DST")
for(i in 1:dim(test1)[2]){
pos = names(test1)[i]
if(pos == "FLEX"){
player = data1[data1$Position %in% c("WR","TE","RB"),]
} else {
player = data1[data1$Position == pos,]
}
name = player$Name[which(player$FPPG == max(player$FPPG))]
salary = salary + player$Salary[which(player$FPPG == max(player$FPPG))]
points = points + player$FPPG[which(player$FPPG == max(player$FPPG))]
data1 = data1[data1$Name != name,]
test1[,i] = name
}
test1$TeamSalary = salary
test1$TotalPoints = points
test2 = rbind(test2,test1)
}
test2
答案 1 :(得分:0)
这也有效
test8 = teams
test8$Name = as.character(test8$Name)
test2=NULL
for(j in 1:(nrow(test8)/9)){
test = test8[1:9,]
test3 = split(test, test$Position)
if(nrow(test3$RB) == 3){
test4 = as.data.frame(cbind(test3$QB$Name, test3$RB$Name[1], test3$RB$Name[2], test3$WR$Name[1], test3$WR$Name[2],test3$WR$Name[3],test3$TE$Name, test3$RB$Name[3], test3$DST$Name[1], test$TeamSalary[1], test$TotalPoints[1]))
} else if(nrow(test3$WR) == 4){
test4 = as.data.frame(cbind(test3$QB$Name, test3$RB$Name[1], test3$RB$Name[2], test3$WR$Name[1], test3$WR$Name[2],test3$WR$Name[3],test3$TE$Name, test3$WR$Name[4], test3$DST$Name[1], test$TeamSalary[1], test$TotalPoints[1]))
} else {
test4 = as.data.frame(cbind(test3$QB$Name, test3$RB$Name[1], test3$RB$Name[2], test3$WR$Name[1], test3$WR$Name[2],test3$WR$Name[3],test3$TE$Name, test3$TE$Name[2], test3$DST$Name[1], test$TeamSalary[1], test$TotalPoints[1]))
}
test2 = rbind(test2,test4)
test8 = test8[-c(1:9),]
}
colnames(test2) = c("QB", "RB", "RB", "WR", "WR", "WR", "TE", "FLEX", "DST", "TeamSalary", "TeamPoints")