Reputation: 118
I'm an R noob, so I know this probably has a simple solution, but I can't figure it out. I'm trying to read in data that is deliminated by spaces. There are 5 columns and 1000 rows. I want to take the mean and median of each row and then calculate the mean-squared error and bias based off of both of mean and median.
I'm struggling to take the mean of individual rows, and I'm not sure what I'm doing wrong.
My data looks like:
X1 X2 X3 X4 X5
-2.3564870e-02 -3.3810429e-01 -3.0566635e+00 -1.1046286e-02 -3.0032159e-01
vals <- read.table("laplace_samples.dat")
rowMeans(vals)
I always get the error:
Error in rowMeans(vals) : 'x' must be numeric
I would prefer to do this in a loop and look at the means individually, but I can't figure out how to do that, either. What I've tried:
for(i in 2:1001){
row = vals[i,1:5]
mn = mean(row)
med = median(row)
..... other code
}
Also, is there I way I can convert it into a matrix so I can index vals[colnumber][rownumber]
or something like that?
Upvotes: 0
Views: 188
Reputation: 697
This questions should be migrated on StackOverFlow. However...
Looks like you have a characters first row with X1 X2 X3 X4 X5. Use:
vals <- read.table("laplace_samples.dat", header = TRUE)
rowMeans(vals)
If the above code does not work try:
vals <- read.table("laplace_samples.dat")
vals <- as.numeric(vals)
rowMeans(vals)
If you want vals
to be a matrix type:
vals <- as.matrix(vals)
Upvotes: 1