Reputation: 11
I am not able to load my data file into R. I have tried every possible function I could find, including so many different variations of read.table and scan.
The file is a .txt file, and contains a block of data. It has no headers, etc. It is simply a set of results contained in a rectangle, with only spaces separating the different results.
When I input the data as if it were a table, and try finding the summary of the data, each row is taken as its own data set. So I end up with 9 different summaries.
I need the data to be recognised as if it were a list of numbers just separated by a comma, but regardless of what I do, I can't seem to do this.
Also, I have a mac (if that effects anything at all).
Any help would be MUCH appreciated. Thank you.
Upvotes: 1
Views: 505
Reputation: 7654
Here is another approach. I created text.txt in Word, the data looking like df below but without row numbers or column names. I read it into R with read.table
.
df <- read.table("~/R/text.txt", sep = "")
df
V1 V2 V3 V4
1 1 3 5 7
2 2 4 6 8
3 1 2 3 4
4 5 6 7 8
library("tidyr") # to do what the `melt` function of reshape2 does
df2 <- gather(df)[-1] # arrange all the values in one variable [remove one column]
value
1 1
2 2
3 1
4 5
5 3
6 4
7 2
8 6
9 5
10 6
11 3
12 7
13 7
14 8
15 4
16 8
summary(df2$value) # calculate the summary statistics
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 2.75 4.50 4.50 6.25 8.00
Upvotes: 2
Reputation: 34288
If I correctly understand your question, then you want to read the following (which is in a file, say ~/tmp/spaced-data.dsv
):
1 2 3
4 5 6
7 8 9
as a single vector: c(1, 2, 3, 4, 5, 6, 7, 8, 9)
.
If your file is not very large, then you can do the following:
> read.csv( text=paste0(readLines('~/tmp/spaced-data.dsv'), collapse=' '),
header=F,
sep=' '
)
V1 V2 V3 V4 V5 V6 V7 V8 V9
1 1 2 3 4 5 6 7 8 9
Upvotes: 1