I have the following data set in Sheet1 of an XLSX file data.xlsx:

enter image description here

Prop1   Prop2   Prop3   Value1  Value2  Value3
A       B       C       1       2       3
D       E       F       4       5       6
G       H       I       7       8       9

I'm reading in the data use the xlsx package via

> df <- read.xlsx(
+     file = 'data.xlsx',
+     sheetName = 'Sheet1',
+     header = TRUE)

Now I want to calculate the median of the entire set of values (1,...,9; across rows 1-3 and columns 4-6). However, I'm unable to coerce the data to be numeric.

> median(as.numeric(df[,c(4:6)]))
Error in median(as.numeric(df[, c(4:6)])) : 
  (list) object cannot be coerced to type 'double'

How can determine the median of this set?

有帮助吗?

解决方案

One approach is to untangle or unlist the data frame before calculating the median:

> df <- data.frame(
+     Prop1 = c('A','B','C'), Prop2 = c('D','E','F'), Prop3 = c('G','H','I'),
+     Value1 = c(1:3), Value2 = c(4:6), Value3 = c(7:9))
> df
  Prop1 Prop2 Prop3 Value1 Value2 Value3
1     A     D     G      1      4      7
2     B     E     H      2      5      8
3     C     F     I      3      6      9
> df.median <- median(unlist(df[,c(4:6)]))
> df.median
[1] 5
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top