-1

I have the dataframe below:

product<-c("ab","ab","ab","ac","ac","ac")
shop<-c("sad","sad","sad","sadas","fghj","xzzv")
week<-c(31,31,32)
category<-c("a","a","a","b","b","b")
tempr<-c(35,35,14,24,14,5)
value<-c(0,0,-6,8,4,0)
store<-data.frame(product,shop,category,tempr,value)

which looks like this:

  product  shop category tempr value
      ab   sad        a    35     0
      ab   sad        a    35     0
      ab   sad        a    14    -6
      ac   sadas      b    24     8
      ac   fghj       b    14     4
      ac   xzzv       b     5     0

I have a question that is probably wrong but I want to subset this dataframe by selecting the second column by name, then droping certain columns(1,3,4) by index number and then select the rest of the columns by index numbers without knowing the limit. Something like:

store2<-store2[,input$lux2,-c(1,3),4:]

Lets say that I drop out columns 1 and 3 keep column by giving the "shop" from my selectInput and then I want all the columns left. This would be the result:

   shop tempr value
   sad    35     0
   sad    35     0
   sad    14    -6
  adas    24     8
  fghj    14     4
  xzzv     5     0
Sal-laS
  • 11,016
  • 25
  • 99
  • 169
firmo23
  • 7,490
  • 2
  • 38
  • 114

2 Answers2

1

You cant mix select by colname, negative select and select by col-index. You have to decide whether to use NAMES or INDEX-numbers.

Something like this would work:

colByName = "shop"
removeByInd = c(1,3)
fromNtoEnd  = 4

ind <- setdiff(
    c( match(colByName, names(store)), fromNtoEnd:ncol(store) ),
    removeByInd
)

store2 <- store[,ind]

#   shop tempr value
#1   sad    35     0
#2   sad    35     0
#3   sad    14    -6
#4 sadas    24     8
#5  fghj    14     4
#6  xzzv     5     0

If you consider using dplyr you can use:

store %>% select(-c(1,3),"shop",4:ncol(.))

which is very close to your "imagination" in your question.

Andre Elrico
  • 10,956
  • 6
  • 50
  • 69
1

I drop out columns 1 and 3 keep column by giving the "shop" from my selectInput and then I want all the columns left.

As i understand from your explanation, and your output, either you use a selectInput, or do not specify anything, the columns will show up in the list. So, we only need to drop the columns which you do not need them.

#droping certain columns(1,3,4)
store[,-c(1,3)]

and the result is:

  shop tempr value
   sad    35     0
   sad    35     0
   sad    14    -6
 sadas    24     8
  fghj    14     4
  xzzv     5     0
Sal-laS
  • 11,016
  • 25
  • 99
  • 169