R - problems converting txt to read in R

Asked

Viewed 124 times

1

I have a '.xls' that I passed to '.txt' for the R reading, no problem. The point is that by opening '.txt' in R with read.table, it shows me as a single table, in a single R position.

> x
        V1
1     X
2    30.80
3     0.00
4     0.00
5     2.10
6     0.00
7     0.00
8     0.00
9    85.40
10    0.00
11    0.20
12    0.00
13    0.00
14    0.00
15    0.00
16    0.00
17    0.00
18    0.20
19    0.00
20   11.70
21    0.00
22   13.60
23    0.00
24   24.70
25   66.80
26    0.00
27    3.70
28   46.20
29  416.10
30   90.80
31    0.00
32    0.00
33    0.00
34   40.10
35   23.60
36   63.20
37   54.30
38   20.20
39   30.00
40   23.00
41    1.80
42    2.00
43   31.20
44   20.20
45  150.60
46   50.80
47    3.30
48    4.00
49   10.40
50    0.70
51    8.60
52    0.00
53    0.00
54    9.30
55    0.00
56    0.00
57  176.70
58    4.90
59   68.80
60   17.70
61    9.80
62    9.60
63    0.00
64   47.00
65    0.00
66    0.00
67    2.10
68    0.00
69  144.60
70   83.10
71   55.30
72    3.50
73    0.00
74    0.00
75    5.50
76   47.80
77   52.60
78  154.10
79   47.60
80   13.40
81    0.00
82   37.10
83    0.00
84    0.00
85    0.00
86   27.40
87    0.00
88    0.00
89   40.60
90   67.80
91    0.00
92    0.00
93   19.00
94  154.30
95   14.60
96    0.00
97    9.20
98    0.00
99    0.00
100   0.00
101   0.00
102   0.00
103 147.70
104 108.40
105  55.80
106  37.60
107  62.80
108  80.00
109  51.40
110  23.60
111  10.80
112  12.00
113   0.00

When I do a simple 'for' so that it puts each value in a position to read R, it changes my values. And it’s not rounding, because the figures aren’t even close.

xnovo<-seq(1,113)

for (i in 1:113){
xnovo[i]<-x[i,1][1]}
> xnovo
  [1] 67 29  1  1 23  1  1  1 60  1  2  1  1  1  1  1  1  2  1  7  1 10  1 26 54
 [26]  1 33 40 39 62  1  1  1 37 25 53 48 22 28 24 19 21 30 22 14 45 31 36  4  3
 [51] 61  1  1 64  1  1 17 44 56 18 66 65  1 41  1  1 23  1 11 58 50 32  1  1 49
 [76] 43 47 15 42  9  1 34  1  1  1 27  1  1 38 55  1  1 20 16 12  1 63  1  1  1
[101]  1  1 13  6 51 35 52 57 46 25  5  8  1

Someone knows how to fix this?

  • 1

    Post also the reading code of the .txt for

2 answers

4

You can solve this problem by asking R skip the first line of the text file, using the argument skip = 1 inside the command read.table:

x <- read.table(file = "file.txt", skip = 1)
dim(x)
summary(x)
## [1] 112   1
summary(x)
## V1        
## Min.   :  0.00  
## 1st Qu.:  0.00  
## Median :  5.20  
## Mean   : 28.96  
## 3rd Qu.: 40.23  
## Max.   :416.10
  • 2

    Much better than my answer, I forgot the skip.

  • 1

    Thank you. The argument skip is super good to use, especially when dealing with data coming from Excel and they have, for example, merged cells.

  • Thank you @Ruibarradas and @Marcusnunes!

  • It’s great to know that my response has helped you in some way. So consider vote and accept the answer, so that in the future other people who experience the same problem have a reference to solve it.

4


The problem is in the first element of the column V1 be an alphabetical value ("X"). So R reads the whole column as being:

  • class "factor" if you have stringsAsFactors = TRUE or have nothing, since this is the default value. This seems to be the case for the question. Factors are encoded internally by R as consecutive integers starting at 1. The first level should be "0.00" and corresponds to the whole 1, then comes the level "0.20" which corresponds to the 2, etc..
  • class "character" if you have stringsAsFactors = FALSE.

In both cases you must remove this value "X" and then convert to numeric.

x <- read.table(file = "dados.txt", header = TRUE)

str(x)
#'data.frame':  113 obs. of  1 variable:
#$ V1: Factor w/ 66 levels "0.00","0.20",..: 66 32 1 1 22 1 1 1 60 1 ...


if(any(x[[1]] == "X")) x <- x[-which(x[[1]] == "X"), , drop = FALSE]
x[[1]] <- as.numeric(as.character(x[[1]]))

Then to create the vector xnovo does not need cycles for or others, just do

xnovo <- x[[1]]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.