if all groups have the same length, like your example, the following can be an alternative approach using base R (with one exception for convinience):
# split df to list by group colum, use transpose and select second row
# while converting the result to numeric
lapply(split(df, df$sp), function(x) as.numeric(t(x)[2, ]))
# returns a named list of four list items, containing the to be data.frame rows
$A
[1] 5.853568 6.030801 5.796363 5.921868 6.131841 5.948130 5.903753 6.059718 5.934419 6.098146
$B
[1] 6.073412 5.982033 6.112947 6.077986 5.862820 5.980009 5.873417 5.939692 6.106947 5.894948
$C
[1] 6.023042 5.982562 6.038372 5.866949 6.001430 6.089119 5.883650 5.923488 5.871431 5.992740
$D
[1] 6.115511 5.911106 5.969717 5.919504 5.978090 6.003362 6.026468 6.066644 6.028981 6.082114
Here you can use many apporaches to get a data.frame
result. One of such is to use sapply() instead of lapply() in combination with a second t() call as well as a as.data.frame() call but you will still have to work the row names to a new column:
as.data.frame(t(sapply(split(df, df$sp), function(x) as.numeric(t(x)[2, ]))))
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
A 5.853568 6.030801 5.796363 5.921868 6.131841 5.948130 5.903753 6.059718 5.934419 6.098146
B 6.073412 5.982033 6.112947 6.077986 5.862820 5.980009 5.873417 5.939692 6.106947 5.894948
C 6.023042 5.982562 6.038372 5.866949 6.001430 6.089119 5.883650 5.923488 5.871431 5.992740
D 6.115511 5.911106 5.969717 5.919504 5.978090 6.003362 6.026468 6.066644 6.028981 6.082114
With the help of the plyr
library (here we are out of base R territory) you just need one aditional function call to get a data.frame with an id column (drawn from list item names). the .id argument is not necessary (try to ommit it), it just defines the name for the new column originate from the list item names:
plyr::ldply(lapply(split(df, df$sp), function(x) as.numeric(t(x)[2, ])), .id = "sp")
sp V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1 A 5.853568 6.030801 5.796363 5.921868 6.131841 5.948130 5.903753 6.059718 5.934419 6.098146
2 B 6.073412 5.982033 6.112947 6.077986 5.862820 5.980009 5.873417 5.939692 6.106947 5.894948
3 C 6.023042 5.982562 6.038372 5.866949 6.001430 6.089119 5.883650 5.923488 5.871431 5.992740
4 D 6.115511 5.911106 5.969717 5.919504 5.978090 6.003362 6.026468 6.066644 6.028981 6.082114
considering the needed aditional steps and precondition of equal length (or work arrounds) to get an equal result I prefer and would recommend the tidyr::pivot_
option