[英]Rename row names from a set of dataframe
我想將這組 dataframe 的行名重命名為:
names
<chr>
1 Reporter_intensity_corrected_0___1
2 Reporter_intensity_corrected_0___2
3 Reporter_intensity_corrected_0___3
4 Reporter_intensity_corrected_1___1
5 Reporter_intensity_corrected_1___2
...
至:
names
<chr>
1 Reporter_intensity_corrected_0
2 Reporter_intensity_corrected_0
3 Reporter_intensity_corrected_0
4 Reporter_intensity_corrected_1
5 Reporter_intensity_corrected_1
...
(去掉后面的 3 個下划線和數字)
我可以嘗試什么?
在dplyr
和stringr
中使用正則表達式
df %>%
mutate(names = str_remove_all(names, "(_+_+_+\\d)")
在基礎 R 中,您可以使用sub
到 3 個下划線和一個數字。
df <- transform(df, names = sub('_{3}\\d+$', '', names))
df
# names
#1 Reporter_intensity_corrected_0
#2 Reporter_intensity_corrected_0
#3 Reporter_intensity_corrected_0
#4 Reporter_intensity_corrected_1
#5 Reporter_intensity_corrected_1
數據
df <- structure(list(names = c("Reporter_intensity_corrected_0___1",
"Reporter_intensity_corrected_0___2", "Reporter_intensity_corrected_0___3",
"Reporter_intensity_corrected_1___1", "Reporter_intensity_corrected_1___2"
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.