for R上的小环的循环

问题描述 投票:0回答:1

在工作站上工作

所以,我有titanic.csv的基本数据集,其第五列为Age。我想做的是将该entire column of age存储在变量中,并在其上运行for循环。当我尝试这样做时,它表明该变量是小标题。

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

tata <- read.csv("titanic.csv")

csv文件与。r文件位于同一目录中,因此在这里读取文件没有任何问题。

在变量x中获得age的第五列

x <- tata[,5]

当我打印x时,我在控制台中得到了这个:

console window

然后,我尝试获得一条多行打印语句,该语句显示:第n个人的年龄:((变量值)] >>

for (age in x) {
  print(paste("The", n , "th person has age:", age))
  n = n + 1
}

我将输出设置为:

  [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行

我希望你能理解我的需求。任何帮助,将不胜感激。

在RSTUDIO上工作,我有titanic.csv的基本数据集,其第五列为Age。我想做的是将整个年龄列存储在变量中,并在其上运行for循环。 ...

r csv dataframe for-loop tibble
1个回答
0
投票

由于您已将数据转换为tibble(即read_csv而非read.csv),因此需要调用

© www.soinside.com 2019 - 2024. All rights reserved.