[英]assign headers based on existing row that is not the same row and column / dataframe in R
题。 如何提取将成为标题的行号? 我想我可以使用索引号将该行指定为标题。
标题是 c(a,b,c)
数据6*4矩阵
v1 v2 v3 v4 #header
d a b c #headera,b,c
d 1 1 1
d 1 1 1
a b c e #headera,b,c
2 2 2 e
2 2 2 e
输出4*3矩阵
a b c #header
1 1 1
1 1 1
2 2 2
2 2 2
我的代码..
str_which(df, 'a') #idetify row number
根据您上面写的内容,您需要依次过滤包含 'a'、'b'、'c' 字母的行,这意味着该序列可能从v1
或v2
。 今后,我相信这将解决您的问题:
# create an indexed data frame
df.with.index <- mutate(df, IDX = 1:n())
# filter the data frame by the condition above, and output the index
dplyr::filter(df.with.index,
(v2 == 'a' & v3 == 'b' & v4 == 'c')
| (v1 == 'a' & v2 == 'b' & v3 == 'c'))$IDX
这将导致:
[1] 1 4
如果您需要测试行是否仅包含字母“a”,您可能需要使用以下命令:
dplyr::filter(df.with.index, (v1 == 'a' | v2 == 'a' | v3 == 'a' | v4 == 'a'))$IDX
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.