通过过滤重新命名因子级别,然后对条件语句进行分组

时间:2017-11-23 00:30:28

标签: r tidyverse

我有一个像这样的数据框:

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 2)
{
    fprintf(stderr, "Usage: recover infile\n");
    return 1;
}

char *infile = argv[1];

// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "Could not open %s.\n", infile);
    return 2;
}

char *block;
block = malloc(sizeof(char)*64);
fread(block, 1, 64, inptr);
printf(" es  %s \n", block);

int position = ftell(inptr);
printf("position %d \n", position);

free(block);
fclose(inptr);

我需要先过滤 data<- data.frame(ID= seq(1,12, 1), plantfam= c(1,1,2,2,1,1,1,1,2,2,3,3), lepsp= c(rep("A", 4), "B", "B", rep("C", 6)), needsmorpho= c(rep("yes", 4),"no", "no", rep("yes", 6))) 的所有needsmorpho。然后,我需要将所有yes分组为相同的lepsp。对于每个唯一的plantfamlepsp匹配,plantfam将被赋予唯一的形态物种名称。要创建一个morphosp名称,我想根据唯一的lepspmorphosp匹配来粘贴lepsp和一个唯一的数字。输出将是:

plantfam

我试过了:

output<- data.frame(ID= seq(1,12, 1), 
                   plantfam= c(1,1,2,2,1,1,1,1,2,2,3,3), 
                   lepsp= c("A_morpho1","A_morpho1","A_morpho2","A_morpho2",
                         "B","B","C_morpho1","C_morpho1",
                         "C_morpho2","C_morpho2","C_morpho3","C_morpho3"), 
                   needsmorpho= c(rep("yes", 4),"no", "no", rep("yes", 6)))

2 个答案:

答案 0 :(得分:1)

这会实现您的目标吗?

library( data.table )
setDT(data)
data[ needsmorpho == "yes", lepsp := paste0(lepsp,"_morphosp",plantfam) ]

答案 1 :(得分:1)

来自case_when的{​​{1}}您可以执行以下操作:

dplyr

返回:

library(tidyverse)

data %>% 
  mutate(lepsp = case_when(needsmorpho == "yes" ~ paste0(lepsp, "_morpho", plantfam),
                           TRUE ~ as.character(lepsp)))