Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing column name as parameter to function in R language [duplicate]

Tags:

r

I have got a function :

aggreg <- function(fileName, param){   
  contents <- read.csv(fileName, header=T)
  #print(contents)  #This displays all contents
  print(contents$param) #gives NULL
}

> aggreg("test.csv","Close.Price")
> NULL

Please guide further. Thanks:)

like image 757
Dhara Avatar asked Jan 18 '11 10:01

Dhara


People also ask

How do I copy column names in R?

Method 1 : Using paste() method In order to modify the column names, the paste function in R can be used. The paste() method, can be used for the concatenation of string vectors together to form a larger string or sentence. The string vector arguments are joined using the separator specified in the paste function.

Can you pass a function as a parameter in R?

In R, you can pass a function as an argument. You can also pass function code to an argument. Then, you can assign the complete code of a function to a new object.

How do I select a column in a function in R?

To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.

Can you have two columns with the same name in R?

Because, usually R dataframes do not allow exact same names (when you create them using data. frame() ). Which means dataframes should not have the same column names.


1 Answers

you need to use another way of accessing the columns in the dataframe than via $. There are two other ways:

1.

print(content[[param]])

2.

print(content[,param])
like image 60
Henrik Avatar answered Oct 04 '22 22:10

Henrik