我有以下代码
tourneySeeds<-read.csv("tourney_seeds.csv")
regSeason<-read.csv("regular_season_compact_results.csv")
playoff_teams <- sort(tourneySeeds$team[which(tourneySeeds$season == "2013")])
...
#Selecting the seeds for season A
playoff_seeds <- tourneySeeds[which(tourneySeeds$season == "2013"), ]
seed_col <- vector()
BPI_col<-vector()
for(i in playoff_teams) {
val <- match(i, playoff_seeds$team)
seed_col <- c(seed_col, playoff_seeds$seed[val])
BPI_col <- c(BPI_col, playoff_seeds$BPI[val])
}
#team_seed <- data.frame("Var1" = playoff_teams, "Freq" =seed_col)
team_seed<-data.frame()
team_seed <- cbind(playoff_teams, seed_col,BPI_col)
head(team_seed$seed_col)
tourneySeeds
数据框看起来像
season seed team BPI
1980 W01 1207 5
1980 W02 1210 8
...
但是,对于head(team_seed$seed_col)
,我收到了消息Error in team_seed$seed_col : $ operator is invalid for atomic vectors
team_seed
设置为包含seed_col
的数据框
答案 0 :(得分:3)
问题相对简单。
team_seed <- data.frame()
team_seed <- cbind(playoff_teams, seed_col, BPI_col)
head(team_seed$seed_col)
如果我们打破这个问题,你会发现问题:
team_seed
playoff_teams
,seed_col
和BPI_col
来创建矩阵。team_seed
,从而删除您在第一行中创建的空数据框。 R只是覆盖了它。head()
team_seed
的一个组件,但R正确地抱怨这是一个原子向量(矩阵是一个带有dim
属性的向量)你想要的是一些变化:
team_seed <- data.frame(playoff_teams, seed_col, BPI_col)
没有理由分配空数据帧。如果你这样做,那么你需要填写数据框,而不是覆盖它。
答案 1 :(得分:0)
将最后两行代码改为:
team_seed <- data.frame(playoff_teams, seed_col,BPI_col)
head(team_seed$seed_col)
您似乎用示例中的矩阵覆盖了data.frame
有效吗?