我如何将一个向量元素分成两个?

时间:2017-10-30 18:26:11

标签: r dataframe split

我有以下代码:

df <-
structure(list(V1 = c("chr1:859582-899582", "chr2:859582-899582", 
"chr1:859582-899582", "chr13:859582-899582", "chr21:859582-899582", 
"chrX:859582-899582"), V2 = c("AHR.pfm", "AIRE.pfm", "AIRE.3.pfm", 
"ALX1.pfm", "ALX1.1.pfm", "ALX1.2.pfm"), V3 = c(33440L, 7387L, 
30639L, 11835L, 16260L, 20686L), V4 = c("-", "+", "-", "+", "+", 
"+"), V5 = c(9.188581, 7.982141, 8.127811, 7.48571, 9.529333, 
9.241755), V6 = c("gcacgcaac", "TCTGGTTCAGTTGGATGC", "aaaaccaaacaaacaaaa", 
"GTAATTGTGTTA", "GTAATTAATTTA", "CTAATTAATTTA"), V7 = c(NA, NA, 
NA, NA, NA, NA)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", 
"V7"), class = "data.frame", row.names = c("1", "2", "3", "4", 
"5", "6"))

这提供了以下输出:

file_list <- list.files(path=folder, pattern="*.txt") ##creates list of all txt. files in folder

file_list

o<- file_list[order(as.numeric(gsub(".+_(\\d+)\\.txt$", "\\1", file_list)))]## orders list in numerical order by ending 

r <- sapply(o, FUN = function(x){

  df = read.table(x, skip=1) ##x is the file name from list 

  mean(df$V4)

}) ###takes mean of selected column from entire list and produces below output

如何将输出转换为矢量/数据帧x = txt文件标签的最后一位数(1100,1080,1090等,y =数值(3.817672e-07,3.893099e-07) ,4.386539e-07等)

1 个答案:

答案 0 :(得分:0)

由于此情况下所有向量元素的长度相同,您可以通过直接提供字符位置来使用substr()函数(仅在base包中。)。

创建问题中给出的输出

vec <- c("172010_001_120VC_0.1_1100.txt   3.817672e-07","172010_001_120VC_0.1_1080.txt   3.893099e-07","172010_001_120VC_0.1_1090.txt   4.386539e-07")

问题中给出的打印输出

vec
# [1] "172010_001_120VC_0.1_1100.txt   3.817672e-07"
# [2] "172010_001_120VC_0.1_1080.txt   3.893099e-07"
# [3] "172010_001_120VC_0.1_1090.txt   4.386539e-07"

使用substr()功能

所需的结果
df <- data.frame(x = substr(vec, 22, 25), y = substr(vec, 33, 44))
df
#      x            y
# 1 1100 3.817672e-07
# 2 1080 3.893099e-07
# 3 1090 4.386539e-07