I have a data frame ex.
temp = data.frame(a=c(1,2,3,4),b=c(6,7,8,9),c=c(10,11,12,13))
> temp
a b c
1 1 6 10
2 2 7 11
3 3 8 12
4 4 9 13
I want to extract the 2nd and 4th row of column “a” and “c”, means:
a c
2 11
4 13
This “a” and “c” can vary during the input and can have more column names, so I put them into a list like,
t <- c("a","c")
output <- c(output,temp[2,t])
output <- c(output,temp[4,t])
But it returned me
> f
$a
[1] 2
$c
[1] 11
$a
[1] 4
$c
[1] 13
What I want is
a c
2 11
4 13
As answered by Ananda Mahto in a comment to my question, I can store the rows and column names in vectors and use those to get the data I want: