简体   繁体   English

如何传递R函数参数来子集列

[英]How to pass an R function argument to subset a column

First I am new here, this is my first post so my apologies in advance if I am not doing everything correct. 首先,我是新来的人,这是我的第一篇文章,因此,如果我做的不正确,请事先道歉。 I did take the time to search around first but couldn't find what I am looking for. 我确实花时间先搜索一下,但是找不到我想要的东西。 Second, I am pretty sure I am breaking a rule in that this question is related to a 'coursera.org' R programming course I am taking (this was part of an assignment) but the due date has lapsed and I have failed for now, I will repeat the subject next month and try again but I am kind of now in damage control trying to find out what went wrong. 其次,我很确定自己正在违反一条规则,因为这个问题与我正在参加的“ coursera.org” R编程课程有关(这是作业的一部分),但是截止日期已经过去,并且我现在失败了,我将在下个月重复该主题,然后重试,但是我现在处于损害控制方面,试图找出问题所在。

Basically below is my code: What I am trying to do is read in data from a series of files. 基本上,下面是我的代码:我想做的是从一系列文件中读取数据。 These files are four columns wide with the titles: Date, nitrate, sulfate and id and contain various rows of data. 这些文件有四列,标题分别是:日期,硝酸盐,硫酸盐和ID,并包含各行数据。

The function I am trying to write should take the arguments of the directory of the files, the pollutant (so either nitrate or sulfate), and the set of numbered files, eg files 1 and 2, files 1 through to 4 etc. The return of the function should be the average value of the selected pollutant across the selected files. 我尝试编写的函数应采用文件目录,污染物(因此为硝酸盐或硫酸盐)以及编号文件集的参数,例如文件1和2,文件1至4等。返回函数的平均值应为选定文件中选定污染物的平均值。

I would call the function using a call like this 我会使用这样的调用来调用函数

pollutantmean("datafolder", "nitrate", 1:3)

and the return should just be a number which is the average in this case of nitrate across data files 1 through to 3 并且返回值应该只是一个数字,在这种情况下,这是数据文件1至3中硝酸盐的平均值

OK, I hope I have provided enough information. 好的,我希望我提供了足够的信息。 Other stuff that may be useful is: 其他有用的东西是:

  • Operating system :Ubuntu 操作系统:Ubuntu
  • Language: R 语言:R
  • Error message received: 收到错误消息:

    Warning message: In is.na(x) : is:na() applied to non(list or vector) of type 'NULL' 警告消息:在is.na(x)中:is:na()应用于类型为“ NULL”的非(列表或向量)

As I say, the data files are a series of files located in a folder and are four columns wide and vary as to the number of rows. 就像我说的那样,数据文件是位于文件夹中的一系列文件,具有四列宽,并且行数有所不同。

My function code is a follows: 我的功能代码如下:

pollutantmean <- function(directory, pollutant, id = 1:5) { #content of the function
#create a list of files, a vector I think
files_list <- dir(directory, full.names = TRUE) 
# Now create an empty data frame
dat <- data.frame()

# Next step is to execute a loop to read all the selected data files into the dataframe
for (i in 1:5) { 
    dat <- rbind(dat, read.csv(files_list[i]))
}

#subsets the rows matching the selected monitor numbers
dat_subset <- dat[dat[, "ID"] == id, ]

#identify the median of the pollutant and ignore the NA values
median(dat_subset$pollutant, na.rm = TRUE)

ok, that is it, through trial and error I am pretty sure the final line of code, the " median(dat_subset$pollutant, na.rm = TRUE) " appears to be the problem. 好的,就是这样,通过反复试验,我很确定代码的最后一行“ median(dat_subset$pollutant, na.rm = TRUE) ”似乎是问题所在。 I pass an argument to the function of pollutant which should be either sulfate or nitrate but it seems the dat_subset$pollutant bit of code is what is not working. 我对污染物的功能(应该是硫酸盐或硝酸盐)传递了一个论点,但似乎dat_subset$pollutant位的代码无法正常工作。 Somehow I am getting the passed pollutant argument to not come into the function body. 我以某种方式使通过的污染物论点不进入功能体。 the dat_subset$pollutant bit should ideally be equivalent to either dat_subset$nitrate or dat_subset$sulfate depending on the argument fed to the function. 理想情况下, dat_subset$pollutant位应等效于dat_subset$nitratedat_subset$sulfate具体取决于传递给函数的参数。

You cannot subset with $ operator if you pass the column name in an object like in your example (where it is stored in pollutant ). 如果像示例中那样在对象中传递列名(将其存储在pollutant ),则不能使用$运算符作为子集。 So try to subset using [] , in your case that would be: 因此,在您的情况下,尝试使用[]进行子集化:

median(dat_subset[,pollutant], na.rm = TRUE)

or 要么

median(dat_subset[[pollutant]], na.rm = TRUE)

Does that work? 那样有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM