The display of your "example" object and the attempts you've made indicate to me that even though you are calling your object a data.frame
, it's actually a matrix
.
My hints that you're actually using a matrix
?
data.frame
s don't generally print the quotes around strings.as.numeric(some_data_frame)
will give you an error about coercing alist
to double.
With that, here's some example data:
example <- structure(c("0.0143", "0.0088", "0.0927", "0.1286",
"---", "0.8692", "---", "0.343", "---"),
.Dim = c(3L, 3L),
.Dimnames = list(c("S01LA", "N01AX", "N05AG"),
c("col1", "col2", "col3")))
example
# col1 col2 col3
# S01LA "0.0143" "0.1286" "---"
# N01AX "0.0088" "---" "0.343"
# N05AG "0.0927" "0.8692" "---"
Here's an approach you can take if that's the case.
example[example == "---"] <- NA ## Replace "---" with `NA`
N <- as.numeric(example) ## Convert to numeric. You can start here
dim(N) <- dim(example) ## Restore the dimensions
dimnames(N) <- dimnames(example) ## Restore the dimnames
colMeans(N, na.rm=TRUE) ## Perform your calculation
# col1 col2 col3
# 0.0386 0.4989 0.3430
Note: You can actually skip the first line, but you'll get a warning
.