拜托,有人知道我为什么能';t在R中的向量内保存数据? checknames
您在将名称添加到数据帧方面做得很好。 如果要在执行函数后获得可用的结果,必须在函数末尾再次调用该表拜托,有人知道我为什么能';t在R中的向量内保存数据? checknames,r,R,您在将名称添加到数据帧方面做得很好。 如果要在执行函数后获得可用的结果,必须在函数末尾再次调用该表 checknames <- function(){ gamers <- c("Rebeca","Lucas","Mario") games <- c("3","1","3") scores <- c("200","100","205") table <- data.frame(gamers,games,scores) r=0 repeat{ print("Name
checknames <- function(){
gamers <- c("Rebeca","Lucas","Mario")
games <- c("3","1","3")
scores <- c("200","100","205")
table <- data.frame(gamers,games,scores)
r=0
repeat{
print("Name Player 1: ")
name1=scan(,what="character",1)
for(i in 1:length(gamers)){
if(name1==gamers[i]){
print("This player is already in the file. Would you like to change the name?")
r=scan(,what="character",1)
}
}
if(r==2){
break
}
if(r==0){
gamers=c(gamers,name1)
name1 <- data.frame(gamers=name1,games="0",scores="100")
table1 <- rbind(table,name1)
print("Nice")
break
}
}
}
checknames()
checknames以下是对代码的一些更改:
在函数外部创建表,除非该表仅在函数内部使用
对于游戏的数量和分数,使用数字而不是字符串是一个很好的方法
您不必在表中循环检查给定的名称是否已经存在
返回新的数据表
将表作为参数强制转换函数。将函数分配给表以保存更改
tbl_游戏我看到了一些小问题,但你提到的主要问题可能是在table1的那行中
checknames <- function(){
code here
table1 #to be returned
}
newTable <- checknames()