建议我有一个data.frame:
df<-mtcars
我现在要做的是选择之前的行 rowname ="Valiant"
和之后的行 rowname = "Datsun 710"
这样输出看起来像:
mpg cyl disp hp drat wt qsec vs am gear carb
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
Hornet 4 Drive 2 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
到目前为止我尝试了什么:
select <- df[which(rownames(df)=="Valiant")-1,] | df[which(rownames(df)=="Datsun 710")+1, ]
select <- df[ which((rownames(df) == "Valiant") -1 | (rownames(df)== "Datsun 710") +1) , ]
select <- df[(rownames(df) == "Valiant")-1 | (rownames(df) == "Datsun 710")+1, ]
select <- df[ (which(rownames(df) == "Valiant") -1) | (which(rownames(df) == "Datsun 710") +1), ]
但其中没有工作虽然单个代码片段有效:
> df[which(rownames(df)=="Valiant")-1,]
mpg cyl disp hp drat wt qsec vs am gear carb
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
> df[which(rownames(df)=="Datsun 710")+1, ]
mpg cyl disp hp drat wt qsec vs am gear carb
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
我错过了什么?
答案 0 :(得分:1)
我想你可以试试
select <- df[ c(which((rownames(df) == "Valiant"))-1 ,which((rownames(df)== "Datsun 710")) +1) , ]
因为which
返回数字,您只需c
所有which
答案 1 :(得分:0)
我想出了以下答案:
book=[
{
bookId:"abcd",
authorId:"1"
},
{
bookId:"def",
authorId:"1"
}
{
bookId:"ghi",
authorId:"2"
}
{
bookId:"kjl",
authorId:"1"
}
];
这对我有用:)!