Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从r中数据帧内的列表中提取数字信息?_R_Dataframe - Fatal编程技术网

如何从r中数据帧内的列表中提取数字信息?

如何从r中数据帧内的列表中提取数字信息?,r,dataframe,R,Dataframe,在名为dfgModsPepFiltered_subset的数据框的第一列中,我有以下类型的条目: A640-P641 = 456.123x 尝试使用以下R脚本从中提取数字信息: dfgModsPepFiltered_subset$AA <- regmatches(dfgModsPepFiltered_subset$Peptide, gregexpr("[[:digit:]]+", dfgModsPepFiltered_subset$Peptide)) 然而,我真正需要的

在名为
dfgModsPepFiltered_subset
的数据框的第一列中,我有以下类型的条目:

A640-P641 = 456.123x
尝试使用以下R脚本从中提取数字信息:

dfgModsPepFiltered_subset$AA <- regmatches(dfgModsPepFiltered_subset$Peptide,
        gregexpr("[[:digit:]]+", dfgModsPepFiltered_subset$Peptide))
然而,我真正需要的是为
“640”
“641”
“456.123”
中的每一个添加一个新列


我尝试了各种未列出的组合,但似乎无法获得正确的格式。

您可以修改
regmatches

 as.data.frame(do.call(`rbind`,
         lapply(regmatches(dfgModsPepFiltered_subset$Peptide,
             gregexpr("[[:digit:].]+", dfgModsPepFiltered_subset$Peptide)), 
                                                        as.numeric))

  #   V1  V2      V3
  #1 640 641 456.123
  #2 620 625 285.400
或者使用
tidyr

library(tidyr)
res <-  extract(dfgModsPepFiltered_subset, Peptide, c('Col1', 'Col2', 'Col3'),
               '[A-Z](\\d+)-[A-Z](\\d+) += +(\\d+\\.\\d+).+', convert=TRUE) 


res
#  Col1 Col2    Col3
#1  640  641 456.123
#2  620  625 285.400

数据
dfgModsPepFiltered_子集全面覆盖!非常有用+1是否有
stringi
的空间?我想知道stir_extract_all_regex是否能达到这个目标。@jazzurro是的,可能是这个
do.call(rbind,lappy(stri_extract_all_regex(dfgModsPepFiltered_subset$Peptide,[0-9.]+'),as.numeric))
谢谢您的支持。
library(tidyr)
res <-  extract(dfgModsPepFiltered_subset, Peptide, c('Col1', 'Col2', 'Col3'),
               '[A-Z](\\d+)-[A-Z](\\d+) += +(\\d+\\.\\d+).+', convert=TRUE) 


res
#  Col1 Col2    Col3
#1  640  641 456.123
#2  620  625 285.400
extract(dfgModsPepFiltered_subset, Peptide, c('Col1', 'Col2', 'Col3'),
        '[^0-9]+([0-9]+)[^0-9]+([0-9]+)[^0-9]+([0-9.]+)[^0-9]+')
library(splitstackshape)
res1 <-  cSplit(dfgModsPepFiltered_subset, 'Peptide', '[^0-9.]', fixed=FALSE)
res1[,names(res1)[!colSums(is.na(res1))], with=FALSE]
#   Peptide_2 Peptide_4 Peptide_7
#1:       640       641   456.123
#2:       620       625   285.400
 as.data.frame(t(sapply(strsplit(dfgModsPepFiltered_subset$Peptide,
                       '[^0-9.]'), function(x) na.omit(as.numeric(x)))))

 #   V1  V2      V3
 #1 640 641 456.123
 #2 620 625 285.400
dfgModsPepFiltered_subset <- data.frame(Peptide= c('A640-P641 = 456.123x',
       'A620-B625 = 285.400x'), stringsAsFactors=FALSE)