Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
拜托,有人知道我为什么能';t在R中的向量内保存数据? checknames_R - Fatal编程技术网

拜托,有人知道我为什么能';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()