将grepl转换为函数

时间:2018-06-02 19:40:12

标签: r lapply grepl

这个想法很简单:确定data.frame的级别并提取具有特定级别的所有行,并将其写入新的data.frame。

所以一个非常糟糕的解决方案应该是这样的。

table <- as.data.frame(table)
table$X4[duplicated(table$X4)]
id_list <-levels(factor(table$X4))
id_df <- as.data.frame(id_list)
row_number <- nrow(id_df)

table$a <- grepl(pattern = id_df[1,1], table$X4)
cow1 <- table[table$a == TRUE, ]
table$b <- grepl(pattern = id_df[2,1], table$X4)
cow2 <- table[table$b == TRUE, ]
table$c <- grepl(pattern = id_df[3,1], table$X4)
cow3 <- table[table$c == TRUE, ]
table$d <- grepl(pattern = id_df[4,1], table$X4)
cow4 <- table[table$d == TRUE, ]

我知道基本功能看起来应该像这样做,并避免复制$粘贴代码

for(i in 1:nrow) 
{
 table$[i] <- grepl(pattern = id_df[i,1], table$X4)
 print(table)
 }

1 个答案:

答案 0 :(得分:0)

您不能只使用//This is view file <div class="panel-body"> <div id="dataTables-example_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer"> <div class="row"> <div class="col-sm-12"> <table width="100%" class="table table-striped table-bordered table-hover dataTable no-footer dtr-inline" id="dataTables-example" role="grid" aria-describedby="dataTables-example_info" style="width: 100%;"> <thead> <tr role="row"> <th>Sr #</th> <th>Name</th> <th>Roll No.</th> <!-- <th>Actions</th> --> <?php $Counter=1; ?> <?php foreach($markslist as $designation) { ?><th><?php echo 'quiz'.$Counter;?></th> <?php $Counter++;} ?> </tr> </thead> <tbody> <?php $Counter1=1; foreach($markslist as $designation) { ?> <tr class="gradeA odd" role="row"> <td class="sorting_1"><?php echo $Counter;?></td> <td class="sorting_1"><?php echo $designation->name;?></td> <td class="sorting_1"><?php echo $designation->rollno;?></td> <td class="sorting_1"><?php echo $designation->'quiz'.$Counter1;?></td> <!-- <td class="center"> <div class="btn-group"> <a href="<?php echo base_url()?>Mid/EditMid/<?php echo $designation->Id;?>"><button class="btn btn-warning">Update</button></a> <a href="<?php echo base_url()?>Mid/DeleteMid/<?php echo $designation->Id;?>"><button class="btn btn-danger">Delete</button></a> </div> </td> --> </tr> <?php $Counter1++; } ?> </tbody> </table></div></div></div> <!-- /.table-responsive --> </div>函数吗?

split()

mylist <- split(iris, iris$Species) 是数据帧的列表,列表项的名称与因子级别名称相对应。

打印单个数据框

mylist

要打印所有新数据框:

mylist$setosa

如果您事先知道因子水平(并且只有1个因子列):

lapply(mylist, print)