如何过滤数据框?
How to filter data frame?
我已经对一些数据进行了两两比较,我的输出如下,现在我喜欢过滤并与低于 0.02 的值进行比较。
d1 d2 d3 d4
d1 NA 0.033 0.323 0.008
d2 NA NA 0.039 0.056
d3 NA NA NA 0.004
d4 NA NA NA NA
我喜欢
d1-d4 0.008
d3-d4 0.004
感谢您的帮助。
你可以这样做(你的数据保存在d
):
# find the row and column index with smaller values
gr <- which(d<0.02, arr.ind = T)
# get the row and col names and paste them together
a1 <- paste(rownames(d)[ gr[, 1]], colnames(d)[ gr[, 2]], sep="-")
# subset the values
a2 <- d[gr]
# and the result
data.frame(a1, a2)
a1 a2
1 d1-d4 0.008
2 d3-d4 0.004
在base
R
vals <- na.omit(unlist(apply(df, 1, function(x) x[x < 0.02])))
as.data.frame(vals)
# vals
#d1.d4 0.008
#d3.d4 0.004
我已经对一些数据进行了两两比较,我的输出如下,现在我喜欢过滤并与低于 0.02 的值进行比较。
d1 d2 d3 d4
d1 NA 0.033 0.323 0.008
d2 NA NA 0.039 0.056
d3 NA NA NA 0.004
d4 NA NA NA NA
我喜欢
d1-d4 0.008
d3-d4 0.004
感谢您的帮助。
你可以这样做(你的数据保存在d
):
# find the row and column index with smaller values
gr <- which(d<0.02, arr.ind = T)
# get the row and col names and paste them together
a1 <- paste(rownames(d)[ gr[, 1]], colnames(d)[ gr[, 2]], sep="-")
# subset the values
a2 <- d[gr]
# and the result
data.frame(a1, a2)
a1 a2
1 d1-d4 0.008
2 d3-d4 0.004
在base
R
vals <- na.omit(unlist(apply(df, 1, function(x) x[x < 0.02])))
as.data.frame(vals)
# vals
#d1.d4 0.008
#d3.d4 0.004