Subset columns based on list of column names and bring the column before it

后端 未结 2 938
日久生厌
日久生厌 2021-02-19 08:59

I have a larger dataset following the same order, a unique date column, data, unique date column, date, etc. I am trying to subset not just the data column by name but the uniqu

2条回答
  •  日久生厌
    2021-02-19 09:30

    How about

    NameList <- c("Fire","Earth")
    
    idx <- match(NameList, names(MAINDF))
    idx <- sort(c(idx-1, idx))
    
    NewDF <- MAINDF[,idx] 
    

    Here we use match() to find the index of the desired column, and then we can use index subtraction to grab the column before it

提交回复
热议问题