Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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 如果在data.table中找到模式,则提取列的一部分_R_Pattern Matching_Data.table - Fatal编程技术网

R 如果在data.table中找到模式,则提取列的一部分

R 如果在data.table中找到模式,则提取列的一部分,r,pattern-matching,data.table,R,Pattern Matching,Data.table,我有一个data.table,我想把一个模式列的向量部分放在其他列中。例如,我有以下data.table: library(data.table) df <- fread('./file') df V1 V2 V3 V4 V5 V6 V7 V8 V9 1: 0 -148 -49 -1 X CAT 5 0 NA 2: 1 -147 -49 5 X FOT 12 0 NA 3: 2 -146 -4

我有一个data.table,我想把一个模式列的向量部分放在其他列中。例如,我有以下data.table:

library(data.table)
df <- fread('./file')
df

        V1   V2  V3 V4 V5      V6 V7 V8 V9
   1:    0 -148 -49 -1  X     CAT  5  0 NA
   2:    1 -147 -49  5  X     FOT  12 0 NA
   3:    2 -146 -49  3  X     FAT  53 0 NA
   4:    3 -145 -48 -2  X     BYE  10 0 NA
   5:    4 -144 -48  0  X     GOO  2  0 NA
结尾模式如下:

V4 == -2 & V6 == 'BYE' 
如果找到这些模式,则从它们之间的V7中提取值。所以12,53,10应该放在一个向量(x)中。

应该这样做:

n <- min(which((df$V2 == -147 & df$V4 == 5 & df$V6 =='FOT') == TRUE)) #determine the start

m <- max(which(df$V4 == -2 & df$V6 == 'BYE'))   #determine the end

x <- df$V7[n:m]

>x
#[1] 12 53 10
n这应该可以做到:

n <- min(which((df$V2 == -147 & df$V4 == 5 & df$V6 =='FOT') == TRUE)) #determine the start

m <- max(which(df$V4 == -2 & df$V6 == 'BYE'))   #determine the end

x <- df$V7[n:m]

>x
#[1] 12 53 10
n这应该可以做到:

n <- min(which((df$V2 == -147 & df$V4 == 5 & df$V6 =='FOT') == TRUE)) #determine the start

m <- max(which(df$V4 == -2 & df$V6 == 'BYE'))   #determine the end

x <- df$V7[n:m]

>x
#[1] 12 53 10
n这应该可以做到:

n <- min(which((df$V2 == -147 & df$V4 == 5 & df$V6 =='FOT') == TRUE)) #determine the start

m <- max(which(df$V4 == -2 & df$V6 == 'BYE'))   #determine the end

x <- df$V7[n:m]

>x
#[1] 12 53 10

n我能想到的一种方法是使用
which=TRUE

start = DT[V2 == -147 & V4 == 5 & V6=='FOT', which=TRUE] ## [1] 2L
end   = DT[V4 == -2 & V6=='BYE', which=TRUE] ## [2] 4L

DT[start:end, V7]
# [1] 12 53 10

请注意,如果有多个匹配项,则将返回所有索引。您可能需要选择相应的
start
end
值。模式不返回任何匹配的情况也是如此。我将让您来解决这些边缘问题。

我可以想到的一种方法是使用
which=TRUE

start = DT[V2 == -147 & V4 == 5 & V6=='FOT', which=TRUE] ## [1] 2L
end   = DT[V4 == -2 & V6=='BYE', which=TRUE] ## [2] 4L

DT[start:end, V7]
# [1] 12 53 10

请注意,如果有多个匹配项,则将返回所有索引。您可能需要选择相应的
start
end
值。模式不返回任何匹配的情况也是如此。我将让您来解决这些边缘问题。

我可以想到的一种方法是使用
which=TRUE

start = DT[V2 == -147 & V4 == 5 & V6=='FOT', which=TRUE] ## [1] 2L
end   = DT[V4 == -2 & V6=='BYE', which=TRUE] ## [2] 4L

DT[start:end, V7]
# [1] 12 53 10

请注意,如果有多个匹配项,则将返回所有索引。您可能需要选择相应的
start
end
值。模式不返回任何匹配的情况也是如此。我将让您来解决这些边缘问题。

我可以想到的一种方法是使用
which=TRUE

start = DT[V2 == -147 & V4 == 5 & V6=='FOT', which=TRUE] ## [1] 2L
end   = DT[V4 == -2 & V6=='BYE', which=TRUE] ## [2] 4L

DT[start:end, V7]
# [1] 12 53 10

请注意,如果有多个匹配项,则将返回所有索引。您可能需要选择相应的
start
end
值。模式不返回任何匹配的情况也是如此。我会让你来解决这些边缘问题。

dt[yourpattern,V7]
dt[yourpattern,V7]
dt[yourpattern,V7]
dt[yourpattern,V7]
?嗯,这对于
数据框架来说很好,但是
数据表的错误使用
——看到
df
被重复了多少次,我感到很不舒服cringe@eddi很抱歉。。嗯,我承认我肯定不是数据表专家。我只是想,对于一个相对简单的操作,它的工作方式可能与data.frame相同。幸运的是,有阿伦的答案和完美的数据。类似桌子的方式:):)哈哈;通过编写
n=df[,min(which((V2=-147&…])
而不是编写此代码
n=df[,min(which((V2==0&…)])]
有时会给我以下错误,即使模式存在。**警告消息:In min(which((V2==0&…):max没有未丢失的参数;返回Inf**max函数也是如此。我如何才能让它正常工作?嗯,这对于
数据帧
,很好,但是
数据表
使用不当-看到重复了多少次
df
,让我感到很困惑cringe@eddi很抱歉……嗯,我承认我当然不是一个data.table专家。我只是想到了一个相对简单的操作,它的工作方式可能与data.frame相同。幸运的是,Arun的答案中有一个完美的data.table-like-king方法:):)哈哈;你可以通过编写
n=df[,min((V2==-147&…)来保持你的代码基本不变,并消除很多额外的
df
instead此代码
n=df[,min(哪((V2==0&…)))]
有时会给我以下错误,即使模式存在。**警告消息:In min(哪((V2==0&…)):max没有未丢失的参数;返回Inf**max函数也是如此。我如何才能让它正常工作?嗯,这对于
数据帧
,很好,但是
数据表
使用不当-看到重复了多少次
df
,让我感到很困惑cringe@eddi很抱歉……嗯,我承认我当然不是一个data.table专家。我只是想到了一个相对简单的操作,它的工作方式可能与data.frame相同。幸运的是,Arun的答案中有一个完美的data.table-like-king方法:):)哈哈;你可以通过编写
n=df[,min((V2==-147&…)来保持你的代码基本不变,并消除很多额外的
df
instead此代码
n=df[,min(哪((V2==0&…)))]
有时会给我以下错误,即使模式存在。**警告消息:In min(哪((V2==0&…)):max没有未丢失的参数;返回Inf**max函数也是如此。我如何才能让它正常工作?嗯,这对于
数据帧
,很好,但是
数据表
使用不当-看到重复了多少次
df
,让我感到很困惑cringe@eddi很抱歉……嗯,我承认我当然不是一个data.table专家。我只是想到了一个相对简单的操作,它的工作方式可能与data.frame相同。幸运的是,Arun的答案中有一个完美的data.table-like-king方法:):)哈哈;你可以通过编写
n=df[,min((V2==-147&…)来保持你的代码基本不变,并消除很多额外的
df
insteadthis code
n=df[,min(哪((V2==0&…)))]
有时会给我以下错误,即使模式存在。**警告消息:在min(哪((V2==0&…))):max没有未丢失的参数;返回Inf**max函数也是如此。我如何才能让它正常工作?