Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.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
如何按id使用grep()数据_R_Lapply - Fatal编程技术网

如何按id使用grep()数据

如何按id使用grep()数据,r,lapply,R,Lapply,我有一个dfRawDat,有两行ID,数据。我想通过id grep()我的数据,例如使用lappy()生成一个新的df,其中数据按id排序为列: 我的df看起来像这样,除了我有>80000行和75个ID: ID数据 abl 564 dlh 78 vho 354 mez 15 abl 662 dlh 69 vho 333 mez 9 . . . 我可以使用grep()函数手动提取数据: ExtRawDat = as.data.frame(RawDat[grep("abl",RawDat$ID),]

我有一个df
RawDat
,有两行
ID,数据
。我想通过id grep()我的数据,例如使用lappy()生成一个新的df,其中数据按id排序为列: 我的df看起来像这样,除了我有>80000行和75个ID:

ID数据
abl 564
dlh 78
vho 354
mez 15
abl 662
dlh 69
vho 333
mez 9
.
.
.

我可以使用grep()函数手动提取数据:

ExtRawDat = as.data.frame(RawDat[grep("abl",RawDat$ID),])
但是,我不想这样做75次,然后cbind()他们。相反,我希望使用lappy()函数将其自动化。我尝试了以下代码的几种变体,但没有得到提供所需输出的脚本

我有一个75个id的向量来循环我的参数

ExtRawDat = as.data.frame(lapply(ProLisV[1:75],function(x){     
Temp1 = RawDat[grep(x,RawDat$ID),]      # The issue is here, the pattern is not properly defined with the X input (is it detrimental that some of the names in the list having spaces etc.?)
Values = as.data.frame(Temp1$data)
list(Values$data)
}))
所需的输出如下所示:

abl dlh vho mez。。。
564     78     354     15
662     69     333     9
.
.
.


如何调整该功能以提供所需的输出?谢谢。

看起来您正在尝试将数据从长格式转换为宽格式。一种简单的方法是使用
tidyr
软件包中的
spread
功能。要使用它,我们需要一个列,因此我们将首先添加一个分组变量:

n.ids <- 4 # With your full data this should be 75
df$group <- rep(1:n.ids, each = n.ids, length.out = nrow(df))
tidyr::spread(df, ID, data)

#   group abl dlh mez vho
# 1     1 564  78  15 354
# 2     2 662  69   9 333

谢谢C.布劳恩!这是一个非常简洁的解决方案,完全符合我的意图。但是有一件事:在75个组之后,组再次从1开始,而tidyr::spread()由于行的标识符重复而抛出一个错误如何告诉rep()函数继续编号直到行结束?谢谢。在rep()函数中,第一个参数应为
1:nrow(RawDat)
df <- read.table(text = "
  ID     data
  abl     564
  dlh     78
  vho     354
  mez     15
  abl     662
  dlh     69
  vho     333
  mez     9", header = T)