0

I would like to know how to select columns containing a word in the header

I have a file like this and i want to get the columns containing the words param1, param2.

fileV1.txt
v1.param1 v1.param2 other.param
value       value      value

fileV2.txt
v2.param1 v2.param2 otherParam
value       value      value

#selectCol <- c("param1", "param2")
#dt.v1   <- data.table::fread(path.v1, select = selectCol, fill = T, header = T)
#dt.v2   <- data.table::fread(path.v2, select = selectCol, fill = T, header = T)
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38
problème0123
  • 841
  • 2
  • 9
  • 23
  • 1
    Are columns always in the same order? Maybe just `fread(path.v1, select = c(1, 2))` ? Related post: https://stackoverflow.com/q/5788117/680068 – zx8754 Aug 12 '19 at 12:23
  • 4
    read in the first line with `scan`. This will return a vector. Then use `grep` to find the locations of the columns that you want. Feed this result to the select argument of `fread`. – lmo Aug 12 '19 at 12:38

2 Answers2

0

You can use

library(dplyr)
select(DT, matches("param[0-9]+"))

You have to replace DT with the name of your dataframe/ table.

Example:

DT = data.table(v2.param1= 1, v2.param2= 2, otherParam= 3)

select(DT, matches("param[0-9]+"))
       v2.param1 v2.param2
1:         1         2

EDIT

If I understand your comment you want to load only columns V2.param1 and V2.param2 from a file. In this case the idea of your code works for me:

library(data.table)
DT = data.table(v2.param1= 1, v2.param2= 2, otherParam= 3)
write.csv(DT, file = "MyData.csv")
selectCol <- c("v2.param1", "v2.param2")
fread(paste0(getwd(), "/MyData.csv"), select= selectCol, fill = T, header = T)
  • excuse me, I was poorly worded my problem, I have a file from which I would like to retrieve the columns containing the words param1 and param2. – problème0123 Aug 12 '19 at 12:17
  • Hello, thank for your help, your code allowed me to better understand how it works but @Aron gave me an example that was more in line with what I wanted. – problème0123 Aug 13 '19 at 07:07
0

This works:

library(dplyr)

selectCol <- c("param1", "param2")

dt.v1 <- data.table::fread(path.v1) %>%
  select(matches(paste(selectCol, collapse = "|")))
Aron Strandberg
  • 3,040
  • 9
  • 15