从iOS开发中快速休息并尝试使用R实现一个非常简单的任务,但我无法弄清楚如何解决此错误,有人可以帮忙吗?
setClass(
"Player",
representation(name="character", position="character", adp="numeric", points="numeric"),
prototype(name=NA_character_, position=NA_character_, adp=NA_real_, points=NA_real_)
)
setwd("/Applications/MAMP/htdocs/fantasy")
playersADP = read.csv("adp_test.csv",stringsAsFactors=FALSE)
print(nrow(playersADP))
players <- 1:nrow(playersADP)
for(i in 1:nrow(playersADP)) {
player <- new("Player",
name = playersADP[i,3],
position = playersADP[i,4],
adp = playersADP[i,1]
)
players[i] = player
}
print(players)
当我运行上面的脚本时,这是我得到的错误&#34;播放器中的错误[i] =播放器:子分配类型中的不兼容类型(从S4到整数)修复&#34;
答案 0 :(得分:1)
您的问题是阵列无法正常工作。
此行创建一个1到nrow(playersADP)
的数组:
players <- 1:nrow(playersADP) # Gives you [1, 2, 3, ...]
这会创建一个整数数组。这些与S4类型不兼容,因此当您尝试将值设置为S4时,会出现类型问题:
players[i] = player # Crashes because players is an array of integers, but player is not an integer
你想要的是这个:
players <- c() # Create an empty array
然后添加到它:
players <- c(players, player)