如何从特定值以上的列中删除元素:

时间:2018-06-03 00:56:02

标签: r

data <- data.frame(IDs = c(4, 0, 5, 18, 24, 1, 2, 
1, 1, 488, 1, 179, 5, 31, 132, 26, 3382))

我想删除ID列中超过400的所有值,因此删除了元素488和3382,数据框最终减少了2行:

    new_data <- data.frame(IDs = c(4, 0, 5, 18, 24, 1, 2, 
1, 1, 1, 179, 5, 31, 132, 26))

我试过玩这样的东西,但似乎并没有起作用:

new_data <- data[data$IDs < 400]

1 个答案:

答案 0 :(得分:0)

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "signal.h"
#include "unistd.h"
#include "fcntl.h"

void prompt(char*);
void execute( char* );
char** parse( char* );

int main( int ac, char* av[] )
{
  char input[255]; // buffer for supporting command

  signal( SIGINT, SIG_IGN ); // ignore ctrl-c

  while(1)
  {
    prompt(input);
    execute( input );
  }
};

void execute( char* str)
{
  int fork_result, status, i = 0,j=0,in=0,out=0;
  char input[100], output[100];
  char** args = parse( str ); // splits the user command into arguments

  fork_result = fork(); // attempt to fork
  if ( fork_result == -1 ) // failure
  {
    perror("Failed to fork\n");
    exit(1);
  }
  else if ( fork_result == 0 ) // I'm the child
  {
    for(i=0;args[i]!='\0';i++)
    {
      if(strcmp(args[i],"<")==0)
      {   
        args[i]=NULL;
        strcpy(input,args[i+1]);
        in=2;   
      }   
      if(strcmp(args[i],">")==0)
      {
        args[i]=NULL;
        strcpy(output,args[i+1]);
        out=2;
      }   
    }

    if (in) 
    {
      j = open(input, O_RDONLY, 0);
      if ((j = open(input, O_RDONLY, 0)) < 0) 
      {
        perror("Couldn't open input file");
        exit(0);
      }
      dup2(j, 0);
      close(j);
    }

    if (out) 
    {
      if ((i= creat(output , 0644)) < 0) 
      {
        perror("Couldn't open the output file");
        exit(0);
      }
      dup2(i, STDOUT_FILENO);
      close(i);
    }

    execvp( args[0], args );
    perror("failed to exec\n");
    exit(2);
  }
  else // I'm the parent
  {
    // wait here
    wait(&status); // wait for child to finish
    free( args ); // free dynamic memory
  }
}

char** parse( char* str )
{
  char** args = malloc( 256 );
  int i = 0;

  args[i] = strtok( str, " " );

  while( args[i] )
  {
    i++;
    args[i] = strtok( NULL, " " );
  }

  return args;
}

void prompt(char* input)
{
  printf("$ "); // print prompt
  fgets( input, 255, stdin );

  input[strlen(input)-1] = '\0'; // overwrite \n with \0

  if ( strcmp( input, "exit" ) == 0 ) // shell command
    exit(0);
}