0

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)
1
  • The data frame is already in memory and not copied or recreated when passed as a parameter to a function. So you can pass the data frame as a single parameter and do the sub-setting in the function itself. Commented Apr 10, 2019 at 13:32

1 Answer 1

1

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)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.