column subsets and indexes in R in modifying a data frame -


for dataset in r, mtcars, trying make subset of data frame, column 1, 2, , 8 , 9. want index columns 8 , 9 can call upon 1 or other in function in order output.

so did

highestmpg <- function(cyl, type) {    type <- mtcars[, c(8, 9)]   df <- mtcars[, c(1, 2, type)] 

i got error far function

error in .subset(x, j) : invalid subscript type 'list'

i don't understand error, can me figure out how circumvent error?

my purpose make data frame in can later input column 2 (cyl), either column 8 or 9 value (0 or 1), in order value column 1 (mpg)

i want rank mtcars cyl, type, in order end organized data frame mpg, in order later find highest mpg, depending on input

ranking <- mtcars[order(cyl, type, mpg)] 

i apologize in advance, still beginner in r, , might not correct in terminology , appreciate advice!

if execute code have given don't error report, different one. assume missed parenthesis when type second line should read:

type <- mtcars[, c(8, 9)] df <- mtcars[, c(1, 2, type)] 

the error message says trying subset list. due type being data.frame (which list in r) prepend list items 1 , 2 list , result:

> c(1, 2, type) [[1]] [1] 1  [[2]] [1] 2  $vs  [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1  $am  [1] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 

it not meaningful use subsetting, , didn't intend to. don't quite understand trying do, can offer now.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -