R-如何扫描输入表中每第n行的第n个变量

时间:2015-08-11 21:28:20

标签: r

我使用read.table输入.dat文件。我想定义一个列表来包含第二个变量,但仅限于从第7行开始的行和之后的第9行。 (因此第7行,第16行,第25行......第10429行)。我可以设置扫描功能或任何其他功能吗?

例如:

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

struct new{
  int x;
  int y;
};

struct score{
  int score;
  struct new* sub_super[];
};

typedef struct score *ext2_filsys;

int ext2fs_get_mem(unsigned long size, void *ptr)
{
  void *pp;
  pp = malloc(size);

  if(!pp)
    return -1;
  memcpy(ptr, &pp, sizeof(pp));
  return 0;
}

/*
 * Free memory
 */
int ext2fs_free_mem(void *ptr)
{
  void *p;

  memcpy(&p, ptr, sizeof(p));
  free(p);
  p = 0;
  memcpy(ptr, &p, sizeof(p));
  return 0;
}

int ext2fs_resize_mem(unsigned long old_size,
                                     unsigned long size, void *ptr)
{
  void *p;

  /* Use "memcpy" for pointer assignments here to avoid problems
   * with C99 strict type aliasing rules. */
  memcpy(&p, ptr, sizeof(p));
  p = realloc(p, size);
  if (!p)
    return -1;
  memcpy(ptr, &p, sizeof(p));
  return 0;
}
int main()
{
  ext2_filsys fs;
  int retval = 0;
  retval = ext2fs_get_mem((sizeof(struct score) + 5 * sizeof(struct new)), &fs);
  if(retval)
    return retval;

  memset(fs, 0, (sizeof(struct score) + 5 * sizeof(struct new)));

  int i;

  for(i=0; i<5; i++) {
    retval = ext2fs_get_mem(sizeof(struct new), &fs->sub_super[i]);
    fs->sub_super[i]->x = i;
    fs->sub_super[i]->y = 15-i;
  }

  for(i=0; i<5; i++)
    printf("Data at %d = %d and address = %p\n", i, fs->sub_super[i]->x, fs->sub_super[i]);

  ext2_filsys tempfs;


  retval = ext2fs_get_mem(sizeof(struct score), &tempfs);
  if(retval)
    return retval;

  *tempfs = *fs;

  retval = ext2fs_get_mem((5 * sizeof(struct new)), &tempfs->sub_super);
  printf("Pointer of tempfs->sub_super = %p\n", tempfs->sub_super);

  for(i=0; i<5; i++) {
    retval = ext2fs_get_mem(sizeof(struct new), &tempfs->sub_super[i]);
    if(retval)
      return retval;
    memcpy(tempfs->sub_super[i], fs->sub_super[i], sizeof(struct new));
  }

  for(i=0; i<5; i++)
    printf("Data at %d = %d and address = %p\n", i, tempfs->sub_super[i]->x, tempfs->sub_super[i]);

  printf("================================================================\n");
  retval = ext2fs_resize_mem(5 * sizeof(struct new), 10 * sizeof(struct new), &tempfs->sub_super);
  if(retval)
    return retval;


  for(i=0; i<10; i++)
    printf("Data at %d = %d and address = %p\n", i, tempfs->sub_super[i]->x, tempfs->sub_super[i]);

 return 0;
}

值以此格式重复。我想在6之后保存一个包含每个值的列表。

1 个答案:

答案 0 :(得分:1)

一种选择是使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section class="slideshow"> <img src="http://placehold.it/350x150" class="bgM show"/> <img src="http://placehold.it/250x150" class="bgM"/> <img src="http://placehold.it/150x150" class="bgM"/> </section>读取文件,然后使用readLines subset读取行,使用seq读取并获取第二列。

read.table

另一个选择是在将工作目录更改为找到输入数据文件的目录后,在linux终端上使用 lines <- readLines('file.txt') v1 <- read.table(text=lines[seq(7, length(lines), by=9)], header=FALSE)[,2] v1 #[1] 25.77898 17.95230 20.06718 命令。我们从第7行开始每隔9行读取第2列(awk),并使用输出创建新文件('file1.txt')

$2

'file1.txt'的内容

 awk 'NR%9==7 {print $2}' file.txt > file1.txt