如何将ascii文件中的数据导入R中的向量?

时间:2019-06-12 19:36:02

标签: r vector import ascii

我不知道如何将ASCII文件中的数据导入向量。 这是我的数据,如下所示:enter image description here

42019 * 1 3738.937 21 * 1 3413.974 20 * 1 3439.52 3287.071 20 * 1 3078.413 3144.528 20 * 1 2695.402 2817.517 20 * 1 2360.609 2535.899 20 * 1 1973.107 2423.964 20 * 1 1853.081 2405.671 20 * 1 1910.165 2377.648 20 * 1 2587.324 21 * 1 2756.768 21 * 1 2759.812 21 * 1 2751.675 21 * 1 2805.15 21 * 1 2860.665 21 * 1 2977.085 21 * 1 3095.787 10473 * 1 4208.778 21 * 1 4263.975 21 * 1 4409.382 21 * 1 4576.375 21 * 1 4310.54 21 * 1 3995.481 3698.043 20 * 1 3501.637 3549.6 20 * 1 3080.588 3484.296 20 * 1  2809.161 3449.817 20 * 1 2584.879 3279.022 4782.676 19 * 1 2242.631 3180.007 4912.585 19 * 1 2150.003 3198.538 4964.336 19 * 1 2453.268 3326.228 4950.596 19 * 1 2572.416 3600.228 5072.902 19 * 1 2561.87 29.5.139.9389.2819 5829。 19 * 1 2914.206 4217.744 5426.702 19 * 1 2842.173 4228.848 5542.386 19 * 1 2729.679 3995.736 5315.139 19 * 1 2791.181 3939.073 4855.432 19 * 1 2906.625 4063.82 4518 3243.637 18 * 1 2804.122 4068.833  4339.896 3472.643 18 * 1 2873.355 4011.656 4219.517 3815.508 18 * 1 3177.821 4065.067 4017.852 4094.735 18 * 1 3523.447 4123.344 3912.661 4196.315 18 * 1 3657.643 4033.419 3906.543 4135.803 18 * 1 3692.156 3685.3689.3689.3689.3679。 3369.104 18 * 1 3511.714 3689.989 3705.094 3226.042 18 * 1 3430.077 3366.809 3930.563 3212.605 18 * 1 3404.816 3195.999 3901.077 3133.576 18 * 1 3373.697 3086.668 3754.917  3014.06 18 * 1 3264.212 2988.194 3597.519 2922.759 18 * 1 3248.592 2898.228 3355.479 2799.94 18 * 1 3344.334 2835.604 2973.876 2693.686 18 * 1 3286.128 2817.112 2689.906 2621.489 16 * 1 4764.407 1 3171.498 2770.696 26.81 2687.168 26.81 2678.678 2784.1678 26.81 2678.678 2784.678 2784.178.678 4122.297 1 3148.35 2680.205 2488.023 2603.228 15 * 1 4091.906 3782.092 1 2984.83 2694.59 2631.974 2647.052 15 * 1 3780.004 3512.631 1 2905.647 2722.086 2701.115 2732.39 15 * 1 3617.159

请注意,42019 * 1表示1被重复42019次。没有列,只有一列。

我尝试了parse.SAScii和read.SASciifunction,但是有错误:

parse.SAScii("text2.data", beginline = 2)

x <- read.SAScii("text2.data", beginline = 2)

Error in if (max(asterisk_slash) < 0) SASinput[j] <- "" else SASinput[j] <- sub(substr(SASinput[j],  : 
  missing value where TRUE/FALSE needed

1 个答案:

答案 0 :(得分:0)

我认为您想要的输出是一个向量,例如42019 * 1的情况变为rep(1,4209),而3738.937的情况保持不变。

就是这样,这是一个选择:

string = "42019*1 3738.937 21*1 3413.974 20*1 3439.52 3287.071 20*1 3078.413 3144.528 20*1 2695.402 2817.517 20*1 2360.609 2535.899 20*1 1973.107 2423.964 20*1 1853.081 2405.671 20*1 1910.165 2377.648 20*1 2003.988 2587.324 21*1 2756.768 21*1 2759.812 21*1 2751.675 21*1 2805.15 21*1 2860.665 21*1 2977.085 21*1 3095.787 10473*1 4208.778 21*1 4263.975 21*1 4409.382 21*1 4576.375 21*1 4310.54 21*1 3995.481 3698.043 20*1 3501.637 3549.6 20*1 3080.588 3484.296 20*1 2809.161 3449.817 20*1 2584.879 3279.022 4782.676 19*1 2242.631 3180.007 4912.585 19*1 2150.003 3198.538 4964.336 19*1 2453.268 3326.228 4950.596 19*1 2572.416 3600.228 5072.902 19*1 2561.87 4009.568 5311.883 19*1 2778.983 4139.855 5447.728 19*1 2934.39 4152.131 5349.623 19*1 2914.206 4217.744 5426.702 19*1 2842.173 4228.848 5542.386 19*1 2729.679 3995.736 5315.139 19*1 2791.181 3939.073 4855.432 19*1 2906.625 4063.82 4518 3243.637 18*1 2804.122 4068.833 4339.896 3472.643 18*1 2873.355 4011.656 4219.517 3815.508 18*1 3177.821 4065.067 4017.852 4094.735 18*1 3523.447 4123.344 3912.661 4196.315 18*1 3657.643 4033.419 3906.543 4135.803 18*1 3692.156 3850.777 3795.754 3977.24 18*1 3689.029 3866.12 3570.128 3656.568 18*1 3631.752 3889.502 3514.744 3369.104 18*1 3511.714 3689.989 3705.094 3226.042 18*1 3430.077 3366.809 3930.563 3212.605 18*1 3404.816 3195.999 3901.077 3133.576 18*1 3373.697 3086.668 3754.917 3014.06 18*1 3264.212 2988.194 3597.519 2922.759 18*1 3248.592 2898.228 3355.479 2799.94 18*1 3344.334 2835.604 2973.876 2693.686 18*1 3286.128 2817.112 2689.906 2621.489 16*1 4764.407 1 3171.498 2770.62 2608.038 2584.703 16*1 4462.878 1 3203.648 2696.728 2506.159 2577.2 16*1 4122.297 1 3148.35 2680.205 2488.023 2603.228 15*1 4091.906 3782.092 1 2984.83 2694.59 2631.974 2647.052 15*1 3780.004 3512.631 1 2905.647 2722.086 2701.115 2732.39 15*1 3617.159"
splitted = as.list(strsplit(string," ")[[1]])

for(i in 1:length(splitted)){
  if(grepl("*",splitted[[i]],fixed=T)){
    aux = strsplit(splitted[[i]],"*",fixed=T)[[1]]
    splitted[[i]] = rep(as.numeric(aux[2]),as.numeric(aux[1]))
  }
  else{
    splitted[[i]] = as.numeric(splitted[[i]])  
  }
}