How to reference data.frame columns in data.frame?

I have a data.frame called serial_to_plot.df that I created by combining several other data.frames together (shown below). Now I want to pull out only the .mm column from the column, so I can build them. So I want to pull out the 3rd column of each data.frame (e.g. p3c3.mm, p3c4.mm, etc.), but I cannot figure out how to do this for all data.frames in the object without looping the name. Is it possible?

I can pull out only one set: for example. series_to_plot.df [[3]] and the other is series_to_plot.df [[10]] (so this is just a list of vectors ..) and I can directly refer to series_to_plot.df $ p3c3.mm, but is there a command to get a vector containing all millimeters from each information. Frame? I expected the index to work as follows: series_to_plot.df [, 3 [3]], but it returns an error in [.data.frame(series_to_plot.df, 3 [3]): undefined selected columns

series_to_plot.df
          p3c3.rd         p3c3.day    p3c3.mm      p3c3.sd                 p3c3.n p3c3.noo p3c3.no_NAs
    1     2010-01-04             0    0.1702531    0.04003364              7                1           0
    2     2010-01-06             2    0.1790594    0.04696674              7                1           0
    3     2010-01-09             5    0.1720404    0.03801756              8                0           0

          p3c4.rd         p3c4.day    p3c4.mm      p3c4.sd                 p3c4.n p3c4.noo p3c4.no_NAs
    1     2010-01-04             0    0.1076581   0.006542157              6                2           0
    2     2010-01-06             2    0.1393447   0.066758781              7                1           0
    3     2010-01-09             5    0.2056846   0.047722862              7                1           0

          p3c5.rd         p3c5.day    p3c5.mm      p3c5.sd                 p3c5.n p3c5.noo p3c5.no_NAs
    1     2010-01-04             0   0.07987147   0.006508766              7                1           0
    2     2010-01-06             2   0.11496167   0.046478767              8                0           0
    3     2010-01-09             5   0.40326471   0.210217097              7                1           0
+3
source share
3 answers

, , , . , . , , -

p c         rd day date mm sd ...
3 3 2010-10-04 ...

, df$mm.

, , R, reshape reshape.

+2

, :

names_with_mm <- grep("mm$", names(series_to_plot.df), value=TRUE)
series_to_plot.df[, names_with_mm]

data.frame , rbind , - :

series_to_plot.df <- rbind(
  cbind(name="p3c3", p3c3),
  cbind(name="p3c4", p3c4),
  cbind(name="p3c5", p3c5)
)

mm .

+4

The R Language Definition contains some useful indexing information (p. 3.4.1), which is very useful.

You can then output the names corresponding to the sequence with the grep () command. Then put it all together as follows:

 dataWithMM <- series_to_plot.df[,grep("[P]", names(series_to_plot.df))]

to expand it a bit, this gets the number of columns that match the mm pattern:

 namesThatMatch <- grep("[mm]", names(series_to_plot.df)

Then we use this list to call the desired columns:

  dataWithMM <- series_to_plot.df[, namesThatMatch ]
+1
source

Source: https://habr.com/ru/post/1734308/


All Articles