UdayK1
UdayK1

Reputation: 1

Specifying multiple columns as input for function

I am trying to write a function to produce a graph and I am trying to specify multiple columns as a input for the functions, I am really new to R so have found myself stuck and unsure on where to get some help.

There are 75 column with data half of which have data from one scenario and half have data from another and I am trying to include both "sets of columns" in the function

I have tried this as the input for my function but did not seem to work

<- function(Data_frame_2,Data_frame_2$V5:v40,Data_frame_2$v41:v80)

Upvotes: 0

Views: 250

Answers (1)

Nick
Nick

Reputation: 286

Welcome. It's always useful to start with some example data.

Here I create a 10 x 80 data frame to match your columns and call it Data_frame_2 containing random variables:

Data_frame_2 <- as.data.frame(matrix(data = runif(10*80),10,80))

You don't need a function to subset the columns but instead use indices "[x,y]" with x representing row numbers and y, the column numbers. In the following example I select columns 1, 4 and 7 from the data frame and all rows within them and call it new_df.

new_df <- Data_frame_2[,c(1,4,7)]
new_df 

In your example, this would look like:

Data_frame_2[,c(5:40,41:80)]

You can also refer to the column names as strings:

Data_frame_2[,c("V1", "V2", "V3")]

Alternatively, to simplify your example, you could drop the columns you don't want:

Data_frame_2[,c(-1:-4)]

Upvotes: 1

Related Questions