R中tibble上的for循环

R中tibble上的for循环,r,csv,dataframe,for-loop,tibble,R,Csv,Dataframe,For Loop,Tibble,在RSTUDIO上工作 因此,我有一个基本的数据集titanic.csv,它的第五列是Age。 我要做的是将整个age列存储在一个变量中,并对其运行for循环。 当我尝试这样做时,它显示变量是一个tible 我用来读取csv文件并将其存储在名为tata的变量中的命令是: 我得到的输出是: [1] "The 1 th person has age 22" "The 1 th person has age 38" [3] "The 1 th person has age 26"

在RSTUDIO上工作

因此,我有一个基本的数据集titanic.csv,它的第五列是Age。 我要做的是将整个age列存储在一个变量中,并对其运行for循环。 当我尝试这样做时,它显示变量是一个tible

我用来读取csv文件并将其存储在名为tata的变量中的命令是:

我得到的输出是:

  [1] "The 1 th person has age 22"   "The 1 th person has age 38"  
  [3] "The 1 th person has age 26"   "The 1 th person has age 35"  
  [5] "The 1 th person has age 35"   "The 1 th person has age 27"  
  [7] "The 1 th person has age 54"   "The 1 th person has age 2"   
  [9] "The 1 th person has age 27"   "The 1 th person has age 14"  
 [11] "The 1 th person has age 4"    "The 1 th person has age 58"
这一直持续到887排


我希望你明白我在这里需要什么。任何帮助都将不胜感激。

由于您已将数据转换为TIBLE,即read_csv和not read.csv,因此需要调用


x由于您已将数据强制转换为TIBLE,即read_csv和not read.csv,因此需要调用


x在R中,您可以不使用循环来完成大多数操作

例如,在这里您可以尝试使用矢量化的粘贴

x <- unlist(x)
paste("The ", seq_along(x), "th person has age ", x)

在R中,您可以不使用循环完成大部分工作

例如,在这里您可以尝试使用矢量化的粘贴

x <- unlist(x)
paste("The ", seq_along(x), "th person has age ", x)

通常这是一个好主意,因为它不会将字符串转换为因子,而且速度更快/更健壮。但您需要小心,df[,somevar]返回的是tibble而不是向量!先生,我得到一个错误作为无效路径参数。首先,在R中找不到函数“pull”。我通过安装并激活一个名为git2r的包解决了这个问题。我很高兴你试图帮助我,但它没有起作用,如果你想帮助我,这将是值得赞赏的进一步拉是dplyr的功能,而不是git2r。。。对不起,我应该把它包括在内!编辑它!通常这是一个好主意,因为它不会将字符串转换为因子,而且速度更快/更健壮。但您需要小心,df[,somevar]返回的是tibble而不是向量!先生,我得到一个错误作为无效路径参数。首先,在R中找不到函数“pull”。我通过安装并激活一个名为git2r的包解决了这个问题。我很高兴你试图帮助我,但它没有起作用,如果你想帮助我,这将是值得赞赏的进一步拉是dplyr的功能,而不是git2r。。。对不起,我应该把它包括在内!编辑它!
  [1] "The 1 th person has age 22"   "The 1 th person has age 38"  
  [3] "The 1 th person has age 26"   "The 1 th person has age 35"  
  [5] "The 1 th person has age 35"   "The 1 th person has age 27"  
  [7] "The 1 th person has age 54"   "The 1 th person has age 2"   
  [9] "The 1 th person has age 27"   "The 1 th person has age 14"  
 [11] "The 1 th person has age 4"    "The 1 th person has age 58"
x <- unlist(x)
paste("The ", seq_along(x), "th person has age ", x)
for (i in seq_along(x)) {
   cat("\nThe ", i, "th person has age ", x[i])
}