简体   繁体   中英

t.test error in R

I have a dataframe with 6 columns. I tired performing a t.test on every row comparing columns 1 - 3 with columns 4 - 6 using the following command:

new.CL.10.ttest <- apply(new.CL, MARGIN = 1, function(m){
  t.test(x = m[1:3], y = m[4:6], alternative = 'two.sided')$p.value
})

I got the following error:

Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") : 
  missing value where TRUE/FALSE needed In addition: Warning messages:
1: In mean.default(x) : argument is not numeric or logical: returning NA
2: In mean.default(y) : argument is not numeric or logical: returning NA

Could I please be advised on how I could resolve this issue?

Here is an example of data set (first 3 rows of a very long list). Numbers are actually floats but I rounded them off for simplicity:

col1    col2    col3   col4     col5    col6
80      100     96     96       93      97
50      45      47     45       54      39
53      44      52     45       68      47

Thanks

Suppose if there no variation in the dataset for a particular row

new.CL[2,] <- 45

Using your code, gives

#Error in t.test.default(x = m[1:3], y = m[4:6], alternative = "two.sided") : 
# data are essentially constant

I guess the error message is different because your original data rows are floating numbers . Using the rounded dataset, a logical index can be created to drop the rows in the original/unrounded dataset. One way to create an index is to check whether the first column is equal to all the columns in the dataset (new.CL[,1]==new.CL) . Because of the recycling , what it does is check elementwise each column with the first column. We get a logical matrix of TRUE/FALSE . In this matrix, some rows have all TRUE ie the data is essentially constant here. To eliminate those rows, do rowSums and check whether it is equal to ncol(new.CL) .

indx <- !rowSums(new.CL[,1]==new.CL)==ncol(new.CL)

Or

 indx <- !!rowSums(new.CL[,1]!=new.CL)

new.Cl1 <- Orig.CL[indx,]

data

new.CL <- structure(list(col1 = c(80L, 50L, 53L), col2 = c(100L, 45L, 44L
), col3 = c(96L, 47L, 52L), col4 = c(96L, 45L, 45L), col5 = c(93L, 
54L, 68L), col6 = c(97L, 39L, 47L)), .Names = c("col1", "col2", 
"col3", "col4", "col5", "col6"), class = "data.frame", row.names = c(NA, 
-3L))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM