0
在我的文件夾中有一堆文件,其文件名是在這個圖案,如何重命名R中for循環下的列?
GSM123445_samples_table.txt
GSM129995_samples_table.txt
...
...
GSM129999_samples_table.txt
每一個文件,該表是在此模式中
Identifier VALUE
10001 0.12323
10002 0.11535
要創建一個數據幀,其中包括只有那些我想要的信息,我使用列表來瀏覽文件夾以選擇我想要的文件並讀取文件表。
我希望我的數據框看起來像這樣
Identifier GSM123445 GSM129995 GSM129999 GSM130095
1 10001 0.12323 0.14523 0.22387 0.56233
2 10002 0.11535 0.39048 0.23437 -0.12323
3 10006 0.12323 0.35634 0.12237 -0.12889
4 10008 0.11535 0.23454 0.21227 0.90098
這是我的代碼
library(dplyr)
for (file in file_list){
if (!exists("dataset")){ # if dataset not exists, create one
dataset <- read.table(file, header=TRUE, sep="\t") #read txt file from folder
x <- unlist(strsplit(file, "_"))[1] # extract the GSMxxxxxx from the name of files
dataset <- rename(dataset, x = VALUE) # rename the column
}
else {
temp_dataset <- read.table(file, header=TRUE, sep="\t") # read file
x <- unlist(strsplit(file, "_"))[1]
temp_dataset <- rename(temp_dataset, x = VALUE)
dataset<-left_join(dataset, temp_dataset, "Reporter.Identifier")
rm(temp_dataset)
}
}
然而,我的結局不工作,我的數據幀這個樣子的
Identifier x.x x.y x.x x.y
1 10001 0.12323 0.14523 0.22387 0.56233
2 10002 0.11535 0.39048 0.23437 -0.12323
顯然,重命名部分失敗了。
我該如何解決這個問題?
您需要標準評估'rename_'而不是非標準eval'rename'。 [讀。](https://cran.r-project.org/web/packages/dplyr/vignettes/nse.html) – alistaire