[R] show structure of selected columns of dataframe

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Wed Sep 15 17:12:39 CEST 2021


Really?

str( df[ , grep(".*_a*.", names(df)) ] )


On September 15, 2021 7:53:17 AM PDT, Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
>Hello,
>I have a dataframe and I would like to browse the information of its
>structure only for a subset of columns (since there are hundreds of
>them).
>For instance, I tried with grepping some columns as in:
>```
>df <- data.frame(var_a1 = c(letters[1:3], letters[1:4]),
>                 var2 = c(LETTERS[1:7]),
>                 var_a2 = c(letters[1:3], letters[1:4]),
>                 var4 = (1:7)^2,
>                 var_a3 = c("light", "light", "heavy", "heavy", "heavy",
>                          "light", "heavy"),
>                 stringsAsFactors = FALSE)
>> grep("*._a*.", names(df))
>[1] 1 3 5
>```
>This tells me that the pattern v_1 is present in the name of columns 1 3 5.
>Would it be possible to get a str() of just these columns?
>
>```
>> str(df)
>'data.frame': 7 obs. of  5 variables:
> $ var_a1: chr  "a" "b" "c" "a" ...
> $ var2  : chr  "A" "B" "C" "D" ...
> $ var_a2: chr  "a" "b" "c" "a" ...
> $ var4  : num  1 4 9 16 25 36 49
> $ var_a3: chr  "light" "light" "heavy" "heavy" ...
>```
>
>Thank you
>

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list