[英]Coverting a set of commands into a function for manipulating dataframe in R
我的數據框(DF1)如下所示:-
【代表,DF1】【1】: https ://i.stack.imgur.com/wZjzR.png
對第 1 行 (MED1) 執行以下操作:
MED1 <- data_frame_merge[1,]
rownames(MED1) <- NULL
MED1 <- t(MED1)
MED1 <- as.data.frame(MED1)
MED1 <- tibble::rownames_to_column(MED1, "Fusion_Type")
MED1$Fusion_Type <- gsub("\\..*", "", MED1$Fusion_Type)
MED1$Fusion_Type <- as.factor(MED1$Fusion_Type)
names(MED1)[names(MED1) == "V1"] <- "TPM"
我明白了:
[MED1,DF] [2]: https ://i.stack.imgur.com/hhMf6.png
基本上,我從第一個數據幀中提取第一行並將其轉換為數據幀以看起來像某種方式(見上圖)。
我的問題是,如何將我對 MED1 所做的轉換為一個函數,以便我可以對第一個數據框中的所有行進行這種轉換。
我多次嘗試使用 for 循環和函數,但總是遇到錯誤。
我嘗試了下面的代碼,但我很確定它充滿了錯誤:
lsEOG<-list()
for (i in 1:nrow(df_main)) {
rownames(df)[i] <- df[i]
df[i] <- df[i]
rownames(df)[i] <- NULL
df[i] <- t(df[i])
df[i] <- as.data.frame(df[i])
df[i] <- tibble::rownames_to_column(df[i], "Fusion_Type")
df[i]$Fusion_Type <- gsub("\\..*", "", df[i]$Fusion_Type)
df[i]$Fusion_Type <- as.factor(df[i]$Fusion_Type)
names(df[i])[names(df[i]) == "V1"] <- "TPM"
lsEOG(df[i])<- df[i]
}
rownames(df)[i] <- NULL 中的錯誤:替換的長度為零(在許多其他中)
您能否通過將代碼轉換為函數來幫助我修復它?
所以這將變成下面,
MED1 <- data_frame_merge[1,]
rownames(MED1) <- NULL
MED1 <- t(MED1)
MED1 <- as.data.frame(MED1)
MED1 <- tibble::rownames_to_column(MED1, "Fusion_Type")
MED1$Fusion_Type <- gsub("\\..*", "", MED1$Fusion_Type)
MED1$Fusion_Type <- as.factor(MED1$Fusion_Type)
names(MED1)[names(MED1) == "V1"] <- "TPM"
變成一個函數? 所以它可以應用於數據框和輸出的所有行
嘗試這個:
library(tidyverse)
as_tibble(data_frame_merge,rownames = "MED") %>%
pivot_longer(cols = -MED, names_to = "Fusion_Type",values_to = "TMP") %>%
mutate(Fusion_Type = stringr::str_extract(Fusion_Type, "(?<=\\d_).*$"))
輸出:
# A tibble: 28 × 3
MED Fusion_Type TMP
<chr> <chr> <dbl>
1 MED1 PML_RARA 57.5
2 MED1 PML_RARA 178.
3 MED1 PML_RARA 20.6
4 MED1 PML_RARA 139.
5 MED10 PML_RARA 158.
6 MED10 PML_RARA 110.
7 MED10 PML_RARA 180.
8 MED10 PML_RARA 128.
9 MED11 PML_RARA 81.8
10 MED11 PML_RARA 91.3
# … with 18 more rows
輸入:
data_frame_merge = structure(list(`TCGA-AB-2991_PML_RARA` = c(57.5155040249228,
157.661027088761, 81.79538436234, 176.603480800986, 188.093456858769,
9.11129987798631, 105.621097609401), `TCGA-AB-3012_PML_RARA` = c(178.483808878809,
110.287002893165, 91.3229470606893, 191.366669069976, 90.6668312381953,
135.514127090573, 114.526680391282), `TCGA-AB-2872_PML_RARA` = c(20.5849365331233,
179.964994080365, 49.217546870932, 8.41190670616925, 65.5841438565403,
190.900729829445, 177.907863212749), `TCGA-AB-2999_PML_RARA` = c(138.56068123132,
128.101362753659, 198.853955324739, 131.141159823164, 141.706093633547,
108.813204942271, 118.828404089436)), class = "data.frame", row.names = c("MED1",
"MED10", "MED11", "MED12", "MED12L", "MED13", "MED13L"))
data_frame_merge
看起來像這樣:
TCGA-AB-2991_PML_RARA TCGA-AB-3012_PML_RARA TCGA-AB-2872_PML_RARA TCGA-AB-2999_PML_RARA
MED1 57.51550 178.48381 20.584937 138.5607
MED10 157.66103 110.28700 179.964994 128.1014
MED11 81.79538 91.32295 49.217547 198.8540
MED12 176.60348 191.36667 8.411907 131.1412
MED12L 188.09346 90.66683 65.584144 141.7061
MED13 9.11130 135.51413 190.900730 108.8132
MED13L 105.62110 114.52668 177.907863 118.8284
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.