使用for循环将几个向量组合成一个向量 [英] Combine several vectors into one vector using a for loop

查看:706
本文介绍了使用for循环将几个向量组合成一个向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我一直在搜索互联网的答案,但找不到我要找的答案:
$ b

分配:我需要循环访问多个文件(由用户指定),提取一列csv文件并将它们粘合在一起,最终计算出指定文件的平均值。

问题

  for(i in 1:whatever){
monitors< - read .csv(list [i],header = T)

/ p>

  cols < -  mons [[pollutant]] 

在这里,我有我的'不洁的'向量(包括NAs)与列的值

  $ b $ return $ result 
$ b code $ <$ pre >

这里是我的问题:我以上面的数字开始返回,每当我尝试从 cols code>结果[I] 或结果[[i]] 我分别得到以下错误:

 对于结果[i] 
要替换的项目数不是替换长度的倍数

对于结果[[i]]
提供的更多元素比替换

现在我意识到这与我的 cols 大于我的结果:现在的问题是:我怎样才能设置这个,以便cols被添加到我的结果向量?

$ b $如果你只是从每个文件中提取和粘贴一列值,我会建议使用连接函数 c()并创建一个 vector ,而不是创建一个 list 类型的对象。

  fnames< -c(fname1,fname2,fname3 )
excol =extractedColumnName

extractedData = c()#初始化向量。键入可以由R自动确定。

for(fname in fnames){

cur < - read.csv(fname,header = T)
extractedData = c(extractedData,cur [ excol])

}

取决于数据如何存储在数据中 na.strings =<用于表示NA的字符串>参数可能需要调用 read.csv 。如果在你想要的列中有字符值,你可能需要在全部读入之后在向量上运行 as.numeric()函数。
There效率更高,编码更密集的方式来加载数据,但对于一个简单的解决方案,不处理太大的数据文件,这种方法应该工作正常。


PS,为了处理NA(假设你不想以任何特殊的方式对待它们),这两种方法中的一种应该可以工作:

<1>:


  extractedDataNoNA = extractedData [! is.na(extractedData)] 
meanResult = mean(extractedDataNoNA)

! is.na(extractedData)创建一个逻辑向量来选择extractData向量中的元素。

<2>:


  meanResult = mean(extractedData,na。 rm = TRUE)


I have a question. I have been trying to search the internet for an answer, but couldn't find the answer I am looking for:

Assignment: I need to loop through several files (specified by the user), extract a column of the csv-files and "glue" them together to finally calculate the mean across the specified files

Problem

for (i in 1:whatever) {
monitors <- read.csv(list[i], header=T)

So, here I read in the file

cols <- mons[[pollutant]]    

Here I have my 'unclean' vector (including NAs) with the values of the columns

result[i] <- c(cols)
}
return(result)
}

And here comes my problem: I initiated return as numeric above and whenever I try to paste the data from colswith either result[i]or result[[i]] i get the following errors respectively:

for result[i]
number of items to replace is not a multiple of replacement length

for result[[i]]
more elements supplied than there are to replace

Now I realize this has to do with my cols being larger than my result: The question now is: how can I set this up so that the cols get added up to my result vector?

解决方案

If you are only extracting and 'gluing' together a single column of values from each file, I would suggest using the concatenation function, c() and creating a vector, instead of creating a list type object. Something along the lines of this should work:

fnames <-c("fname1","fname2","fname3")
excol="extractedColumnName"

extractedData = c() #initialize the vector. Typing can be determined by R automatically. 

for(fname in fnames){

   cur <- read.csv(fname, header=T)
   extractedData = c(extractedData, cur[,excol])

}

Depending on how NAs are stored in your data files, an na.strings = "<the string used to indicate NA>" argument may be necessary for the call to read.csv. If there are character values in the columns you want, you may need to run the as.numeric() function on the vector after all is read in. There are more efficient, more coding-intensive way to load the data, but for a simple solution, not dealing with too large of data files, this method should work fine.

PS, To deal with the NAs (assuming you do not want to treat them in any special way) one of these two approaches should work:

1):

extractedDataNoNA = extractedData[ ! is.na(extractedData) ]
meanResult = mean(extractedDataNoNA)

The ! is.na(extractedData) creates a logical vector to select elements in the extractedData vector.

2):

meanResult = mean(extractedData, na.rm=TRUE)

这篇关于使用for循环将几个向量组合成一个向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆