user10915156
user10915156

Reputation:

Read in multi-dimensional array into R

I have a CSV file in which there is a table of data for every year:

2006
     men women
yes  34  15
no   2   187
2007
     men women
yes  12  88
no   465 NA

The actual file looks like this:

;;men;women
2006;yes;34;15
2006;no;2;187
2007;yes;12;88
2007;no;465;-

How can I read this file into R, either as one matrix, one array, or a series of data frames (one for each year)?

Upvotes: 0

Views: 468

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521093

I would recommend just using read.csv here, with semicolon as the separator:

your_df <- read.csv(file="path/to/your/file.csv", sep=";", stringsAsFactors=FALSE)
names(your_df) <- c("year", "answer", "men", "women")

This reads in your CSV file, and then assigns column names to all four columns (your original data seems to be lacking names for the first two columns).

If you need to do year level analyses, you can work with this data frame as is.

Upvotes: 1

Related Questions