Indexing an entire row with `[` as a function

Art*_*lov 5 r

I would like to use `[` as a function argument in lapply to index full rows. It is fairly straightforward to do this with individual elements:

lapply( list(iris, mtcars), `[`, 1, 3 )
# [[1]]
# [1] 1.4
#
# [[2]]
# [1] 160
Run Code Online (Sandbox Code Playgroud)

However, I can't seem to figure out the prefix equivalent to indexing an entire row of a data frame:

lapply( list(iris, mtcars), `[`, 1 )     # Seems to index columns
lapply( list(iris, mtcars), `[`, i=1 )   # ...same?
Run Code Online (Sandbox Code Playgroud)

I know I can always define a new function

lapply( list(iris, mtcars), function(x) {x[1,]} )   # Desired behavior
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有办法以x[1,]前缀形式获得等效的结果。

tmf*_*mnk 5

你可以做:

lapply(list(iris, mtcars), "[", 1, )

[[1]]
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa

[[2]]
          mpg cyl disp  hp drat   wt  qsec vs am gear carb
Mazda RX4  21   6  160 110  3.9 2.62 16.46  0  1    4    4
Run Code Online (Sandbox Code Playgroud)

后面的空白1,专用于列。如果要整行,则为空白。如果您这样做:

lapply(list(iris, mtcars), "[", 1, 1)
Run Code Online (Sandbox Code Playgroud)

它返回:

[[1]]
[1] 5.1

[[2]]
[1] 21
Run Code Online (Sandbox Code Playgroud)

那就是第一列的第一行。