用R根据行名分隔几个特殊行
Separate several special rows based on row names by R
我有一个 table,我想按行名称分隔一些行。
phi psi
A-MET-0 NA 158.0945
A-VAL-1 -144.914 137.4733
A-LEU-2 -95.021 149.7834
A-SER-3 -82.6826 166.6769
B-GLU-4 -62.5314 -36.2634
H-GLY-5 -59.6463 -42.0355
A-GLU-6 -68.6914 -40.2354
AB-TRP-7 -63.5297 -36.769
A-GLN-8 -64.8056 -38.5128
我会按照下面的命令来分隔特定的行
tor$tbl["A-LEU-2",]
但是当我尝试分隔几行时,出现错误。
我使用以下命令分隔几行。
tor$tbl["A-LEU-2":"AB-TRP-7", ]
如何根据行名从 table 中分隔几行?
谢谢指导。
这是执行此操作的函数 -
subset_by_rowname_range <- function(data, start_name, end_name) {
rn <- rownames(data)
data[match(start_name, rn) : match(end_name, rn), ]
}
subset_by_rowname_range(df, "A-LEU-2", "AB-TRP-7")
# phi psi
#A-LEU-2 -95.0210 149.7834
#A-SER-3 -82.6826 166.6769
#B-GLU-4 -62.5314 -36.2634
#H-GLY-5 -59.6463 -42.0355
#A-GLU-6 -68.6914 -40.2354
#AB-TRP-7 -63.5297 -36.7690
subset_by_rowname_range(df, "H-GLY-5", "A-GLN-8")
# phi psi
#H-GLY-5 -59.6463 -42.0355
#A-GLU-6 -68.6914 -40.2354
#AB-TRP-7 -63.5297 -36.7690
#A-GLN-8 -64.8056 -38.5128
数据
df <- structure(list(phi = c(NA, -144.914, -95.021, -82.6826, -62.5314,
-59.6463, -68.6914, -63.5297, -64.8056), psi = c(158.0945, 137.4733,
149.7834, 166.6769, -36.2634, -42.0355, -40.2354, -36.769, -38.5128
)), class = "data.frame", row.names = c("A-MET-0", "A-VAL-1",
"A-LEU-2", "A-SER-3", "B-GLU-4", "H-GLY-5", "A-GLU-6", "AB-TRP-7",
"A-GLN-8"))
一个简单的解决方案是插入行编号,它可以用作 slice
:
的索引
df %>%
mutate(row = row_number()) %>%
slice(1:8)
或者,这也可行:这里,which
获取 row.names
' 索引,slice
可以通过该索引进行过滤:
df %>%
slice(which(grepl("A-LEU-2", row.names(df))):which(grepl("AB-TRP-7", row.names(df))))
我有一个 table,我想按行名称分隔一些行。
phi psi
A-MET-0 NA 158.0945
A-VAL-1 -144.914 137.4733
A-LEU-2 -95.021 149.7834
A-SER-3 -82.6826 166.6769
B-GLU-4 -62.5314 -36.2634
H-GLY-5 -59.6463 -42.0355
A-GLU-6 -68.6914 -40.2354
AB-TRP-7 -63.5297 -36.769
A-GLN-8 -64.8056 -38.5128
我会按照下面的命令来分隔特定的行
tor$tbl["A-LEU-2",]
但是当我尝试分隔几行时,出现错误。 我使用以下命令分隔几行。
tor$tbl["A-LEU-2":"AB-TRP-7", ]
如何根据行名从 table 中分隔几行?
谢谢指导。
这是执行此操作的函数 -
subset_by_rowname_range <- function(data, start_name, end_name) {
rn <- rownames(data)
data[match(start_name, rn) : match(end_name, rn), ]
}
subset_by_rowname_range(df, "A-LEU-2", "AB-TRP-7")
# phi psi
#A-LEU-2 -95.0210 149.7834
#A-SER-3 -82.6826 166.6769
#B-GLU-4 -62.5314 -36.2634
#H-GLY-5 -59.6463 -42.0355
#A-GLU-6 -68.6914 -40.2354
#AB-TRP-7 -63.5297 -36.7690
subset_by_rowname_range(df, "H-GLY-5", "A-GLN-8")
# phi psi
#H-GLY-5 -59.6463 -42.0355
#A-GLU-6 -68.6914 -40.2354
#AB-TRP-7 -63.5297 -36.7690
#A-GLN-8 -64.8056 -38.5128
数据
df <- structure(list(phi = c(NA, -144.914, -95.021, -82.6826, -62.5314,
-59.6463, -68.6914, -63.5297, -64.8056), psi = c(158.0945, 137.4733,
149.7834, 166.6769, -36.2634, -42.0355, -40.2354, -36.769, -38.5128
)), class = "data.frame", row.names = c("A-MET-0", "A-VAL-1",
"A-LEU-2", "A-SER-3", "B-GLU-4", "H-GLY-5", "A-GLU-6", "AB-TRP-7",
"A-GLN-8"))
一个简单的解决方案是插入行编号,它可以用作 slice
:
df %>%
mutate(row = row_number()) %>%
slice(1:8)
或者,这也可行:这里,which
获取 row.names
' 索引,slice
可以通过该索引进行过滤:
df %>%
slice(which(grepl("A-LEU-2", row.names(df))):which(grepl("AB-TRP-7", row.names(df))))