extracting missing elements after comparing two data frames in R - r
So I have a master list of 96 elements.
masterlist <- list(c(paste0(rep(LETTERS[1:8], each=12), rep(1:12, 8))))
And I have a list of 26 data frames, where each data frames contains any or all of the elements from the master list. I called these the rowcol.list. From here, I used the following code to see which of the data frames in my list of 26 data frames are different from the master list.
rowcol.list[!(rowcol.list%in%masterlist)]
The above code returned 10 data frames from the list of 26 data frames, as shown below:
$p01_control.txt
[1] "A1" "A2" "A3" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8" "B9"
[21] "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4" "D5"
[41] "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12" "F1"
[61] "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "G9"
[81] "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p03_control.txt
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
[21] "B9" "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4"
[41] "D5" "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12"
[61] "F1" "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G9"
[81] "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p04_pq.txt
[1] "A1" "A2" "A3" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8" "B9"
[21] "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4" "D5"
[41] "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12" "F1"
[61] "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "G9"
[81] "G10" "G11" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p07_docetaxel.txt
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
[21] "B9" "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4"
[41] "D5" "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12"
[61] "F1" "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G9"
[81] "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p08_docetaxel_b.txt
[1] "E10" "E11" "E12" "F1" "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5"
[21] "G6" "G7" "G8" "G9" "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p08_docetaxel.txt
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
[21] "B9" "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4"
[41] "D5" "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8"
$p12_dactinomycin.txt
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
[21] "B9" "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4"
[41] "D5" "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "F1"
[61] "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "G9"
[81] "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p16_cisplatin.txt
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
[21] "B9" "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4"
[41] "D5" "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12"
[61] "F1" "F2" "F3" "F4" "F5" "F6" "F7" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "G9"
[81] "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p18_cisplatin_b.txt
[1] "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8" "B9" "B10" "B11" "B12" "C1" "C2"
[21] "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4" "D5" "D6" "D7" "D8" "D9" "D10"
[41] "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12" "F1" "F2" "F3" "F4" "F5" "F6"
[61] "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "G9" "G10" "G11" "G12" "H1" "H2"
[81] "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p18_cisplatin.txt
[1] "A1" "A2" "A3" "A4" "A5"
Instead of seeing the elements that each of those data frames above have because they are different from the master list, I want to see which elements the data frames do NOT have when they are compared to the master list. As a short example, the first data frame that's different, $p01_control.txt should show "A4" because that's what it is missing. (I only know that it's missing "A4" because I went through the list and found which one was missing).
Please let me know how I can make the problem more clear if it's confusing.
Thank you!
I would use setdiff. you can try something like this:
lapply(rowcol.list,function(x) setdiff(masterlist[[1]],x))
Related
Getting dimension values from matrix in R
Suppose this is the matrix I'm working with: What is the R code I need to use to get the dimention coordinates of a value? Like, if I wanted to get the coordinates of where the value is '0' , it should return (A,A) , (B,B) , (C,C) , etc... I couldn't find an answer anywhere..
A crude way could be: nms <- dimnames(distance) mat<- which(distance == 0, TRUE) cbind(nms[[1]][mat[,1]], nms[[2]][mat[,2]]) [,1] [,2] [1,] "A" "A" [2,] "B" "B" [3,] "C" "C" [4,] "D" "D" [5,] "E" "E" [6,] "F" "F" [7,] "G" "G" Or even do.call(outer, c(dimnames(distance), paste))[which(distance == 0, TRUE)] [1] "A A" "B B" "C C" "D D" "E E" "F F" "G G" distance <- structure(c(0, 5, 6, 9, 10, 8, 8, 5, 0, 1, 10, 11, 7, 9, 6, 1, 0, 9, 10, 6, 8, 9, 10, 9, 0, 3, 7, 7, 10, 11, 10, 3, 0, 4, 4, 8, 7, 6, 7, 4, 0, 2, 8, 9, 8, 7, 4, 2, 0), .Dim = c(7L, 7L), .Dimnames = list( c("A", "B", "C", "D", "E", "F", "G"), c("A", "B", "C", "D", "E", "F", "G")))
We can use mapply + dinames + which like below (thank #Onyambu's data) mapply( `[`, dimnames(distance), data.frame(which(distance == 0, arr.ind = TRUE)) ) which gives [,1] [,2] [1,] "A" "A" [2,] "B" "B" [3,] "C" "C" [4,] "D" "D" [5,] "E" "E" [6,] "F" "F" [7,] "G" "G"
R, haven_labelled: Get label as value
I have a vector with the following structure: structure(c(2, 6, 3, 6, 1, 5, 1, 1, 3, 3, 1, 2, 3, 1, 3, 2, 3, 8, 2, 3, 1, 8, 1, 1, 3, 1, 4, 3, 3, 12, 1, 4, 4, 3, 3, 2, 1, 2, 3, 1, 4, 7, 4, 1, 4, 7, 3, 2, 3, 2, 4, 1, 3, 4, 3, 4, 2, 2, 2, 2, 4, 1, 1, 1, 4, 2, 1, 4, 1, 1, 1, 1, 4, 2, 8, 1, 3, 3, 21, 4, 2, 9, 1, 4, 3, 1, 1, 2, 2, 2, 4, 3, 4, 4, 4, 4, 1, 2, 2, 5, 1, 1, 2, 4, 4, 3, 5, 1, 2, 23, 2, NA, NA, NA), label = "Please indicate the year and month of business establishment. - year", format.spss = "F40.0", display_width = 0L, class = c("haven_labelled", "vctrs_vctr", "double"), labels = c(`2021` = 1, `2020` = 2, `2019` = 3, `2018` = 4, `2017` = 5, `2016` = 6, `2015` = 7, `2014` = 8, `2013` = 9, `2012` = 10, `2011` = 11, `2010` = 12, `2009` = 13, `2008` = 14, `2007` = 15, `2006` = 16, `2005` = 17, `2004` = 18, `2003` = 19, `2002` = 20, `2001` = 21, `2000` = 22, `1999` = 23, `1998` = 24, `1997` = 25, `1996` = 26, `1995` = 27, `1994` = 28, `1993` = 29, `1992` = 30, `1991` = 31, `1990` = 32, `1989` = 33, `1988` = 34, `1987` = 35, `1986` = 36, `1985` = 37, `1984` = 38, `1983` = 39, `1982` = 40, `1981` = 41, `1980` = 42, `1979` = 43, `1978` = 44, `1977` = 45, `1976` = 46, `1975` = 47, `1974` = 48, `1973` = 49, `1972` = 50, `1971` = 51, `1970` = 52, `1969` = 53, `1968` = 54, `1967` = 55, `1966` = 56, `1965` = 57)) What I would like to have is a new vector with each value replaced by its label. So, for example, the first four entries in the new column would be (instead of 2, 6, 3, 6...) "2020", "2016", "2019", "2016"... Any ideas on how to solve this economically? Thank you very much!
Try names(attr(df,"labels")[match(df,attr(df,"labels"))]) [1] "2020" "2016" "2019" "2016" "2021" "2017" "2021" "2021" "2019" "2019" "2021" "2020" "2019" [14] "2021" "2019" "2020" "2019" "2014" "2020" "2019" "2021" "2014" "2021" "2021" "2019" "2021" [27] "2018" "2019" "2019" "2010" "2021" "2018" "2018" "2019" "2019" "2020" "2021" "2020" "2019" [40] "2021" "2018" "2015" "2018" "2021" "2018" "2015" "2019" "2020" "2019" "2020" "2018" "2021" [53] "2019" "2018" "2019" "2018" "2020" "2020" "2020" "2020" "2018" "2021" "2021" "2021" "2018" [66] "2020" "2021" "2018" "2021" "2021" "2021" "2021" "2018" "2020" "2014" "2021" "2019" "2019" [79] "2001" "2018" "2020" "2013" "2021" "2018" "2019" "2021" "2021" "2020" "2020" "2020" "2018" [92] "2019" "2018" "2018" "2018" "2018" "2021" "2020" "2020" "2017" "2021" "2021" "2020" "2018" [105] "2018" "2019" "2017" "2021" "2020" "1999" "2020" NA NA NA
Sequence of Months in Words r?
Let's say I want to have a vector that contains each month in order starting with March 2012. I want that month and the next 33. How would I do this?
With base R: dates <- seq.Date(from = as.Date("2012-03-01"), length.out = 33, by = "month") format(dates, "%B %Y") #> [1] "March 2012" "April 2012" "May 2012" "June 2012" #> [5] "July 2012" "August 2012" "September 2012" "October 2012" #> [9] "November 2012" "December 2012" "January 2013" "February 2013" #> [13] "March 2013" "April 2013" "May 2013" "June 2013" #> [17] "July 2013" "August 2013" "September 2013" "October 2013" #> [21] "November 2013" "December 2013" "January 2014" "February 2014" #> [25] "March 2014" "April 2014" "May 2014" "June 2014" #> [29] "July 2014" "August 2014" "September 2014" "October 2014" #> [33] "November 2014" Created on 2019-01-27 by the reprex package (v0.2.1)
I found this You can do this: df <- ymd("2012-03-01")+ months(0:33) df Give me: [1] "2012-03-01" "2012-04-01" "2012-05-01" "2012-06-01" "2012-07-01" "2012-08-01" "2012-09-01" [8] "2012-10-01" "2012-11-01" "2012-12-01" "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01" [15] "2013-05-01" "2013-06-01" "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01" [22] "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01" [29] "2014-07-01" "2014-08-01" "2014-09-01" "2014-10-01" "2014-11-01" "2014-12-01
R: If statement for multiple conditions based on list/vector
Say I have an n-vector that I want to apply an if-condition of based on multiple conditions stored in another vector/list, how can I loop and assign the names to my original vector? set.seed(1234) # Data b <- rpois(100, 4) # Condition a <- 1:10 names(a) <- paste0(seq(10,100, by = 10),'%')
Is this what you want? set.seed(1234) # n-vector b <- rpois(100, 4) # multiple conditions stored in another vector/list a <- 1:10 names(a) <- paste0(seq(10,100, by = 10),'%') # loop and assign the names to the original vector for(i in unique(a)){ b <- ifelse(b==i,names(a[a==i]),b) } b [1] "20%" "40%" "40%" "40%" "60%" "50%" "0" "20%" "50%" "40%" "50%" "40%" "30%" "70%" "30%" "60%" "30%" "30%" [19] "20%" "20%" "30%" "30%" "20%" "10%" "20%" "60%" "40%" "70%" "60%" "10%" "40%" "30%" "30%" "40%" "20%" "50%" [37] "20%" "30%" "100%" "60%" "40%" "50%" "30%" "40%" "30%" "40%" "50%" "40%" "30%" "50%" "10%" "30%" "50%" "40%" [55] "20%" "40%" "40%" "50%" "20%" "60%" "60%" "10%" "30%" "0" "30%" "50%" "30%" "40%" "10%" "40%" "20%" "70%" [73] "0" "50%" "10%" "40%" "30%" "10%" "30%" "50%" "70%" "40%" "20%" "40%" "20%" "70%" "30%" "30%" "20%" "70%" [91] "20%" "70%" "20%" "20%" "20%" "40%" "30%" "10%" "30%" "50%"
If we need to have a named b vector based on 'a', use the match names(b) <- names(a)[match(b, a)] names(b) #[1] "20%" "40%" "40%" "40%" "60%" "50%" NA "20%" "50%" "40%" "50%" "40%" "30%" "70%" "30%" #[16] "60%" "30%" "30%" "20%" "20%" "30%" "30%" "20%" "10%" "20%" "60%" "40%" "70%" "60%" "10%" #[31] "40%" "30%" "30%" "40%" "20%" "50%" "20%" "30%" "100%" "60%" "40%" "50%" "30%" "40%" "30%" #[46] "40%" "50%" "40%" "30%" "50%" "10%" "30%" "50%" "40%" "20%" "40%" "40%" "50%" "20%" "60%" #[61] "60%" "10%" "30%" NA "30%" "50%" "30%" "40%" "10%" "40%" "20%" "70%" NA "50%" "10%" #[76] "40%" "30%" "10%" "30%" "50%" "70%" "40%" "20%" "40%" "20%" "70%" "30%" "30%" "20%" "70%" #[91] "20%" "70%" "20%" "20%" "20%" "40%" "30%" "10%" "30%" "50%" Note that there are elements in 'b' that are not in 'a'. It is not clear what should be the names for that elements. By default, the match returns NA for those elements and the names for that elements will be NA. If we need to change that, we could do it
T.test between two data sets - row by row
I think that title explains everything. I would like to do t.test between two data sets. I would like to compare row by row. Let's use mtcars for that and slightly modified mtcars_mod. structure(list(mpg = c(21, 25, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 24.8, 19.2, 17.8, 16.4, 17.3, 15.2, 10.4, 10.4, 14.7, 32.4, 36.4, 31.9, 21.5, 15.5, 15.2, 13.3, 19.2, 27.3, 26, 30.4, 15.8, 29.7, 15, 21.4), cyl = c(6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 7, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 4, 8, 6, 8, 4), disp = c(160, 160, 108, 258, 360, 225, 360, 146.7, 140.8, 167.6, 167.6, 275.8, 275.8, 275.8, 6, 460, 440, 78.7, 75.7, 71.1, 120.1, 318, 304, 350, 400, 79, 15, 97, 351, 145, 301, 121), hp = c(110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180, 205, 215, 230, 66, 52, 65, 97, 150, 150, 245, 175, 66, 91, 113, 264, 175, 335, 109), drat = c(3.9, 3.9, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92, 3.07, 3.07, 3.07, 2.93, 3, 3.23, 4.08, 4.93, 4.22, 3.7, 2.76, 3.15, 3.73, 3.08, 4.08, 4.43, 3.77, 4.22, 3.62, 3.54, 4.11 ), wt = c(2.62, 2.875, 2.32, 7, 3.44, 3.46, 3.57, 3.19, 3.15, 3.44, 3.44, 4.07, 3.73, 3.78, 5.25, 5.424, 5.345, 2.2, 1.615, 1.835, 2.465, 3.52, 3.435, 3.84, 3.845, 1.935, 2.14, 1.513, 3.17, 2.77, 6, 2.78), qsec = c(16.46, 17.02, 18.61, 114, 17.02, 20.22, 15.84, 12, 22.9, 18.3, 18.9, 17.4, 17.6, 18, 17.98, 17.82, 17.42, 19.47, 18.52, 19.9, 20.01, 16.87, 32, 15.41, 17.05, 18.9, 16.7, 16.9, 14.5, 15.5, 14.6, 18.6 ), vs = c(0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1), am = c(1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1), gear = c(4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5, 4), carb = c(4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2, 2, 4, 2, 1, 2, 2, 4, 6, 8, 2)), .Names = c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"), row.names = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout", "Valiant", "Duster 360", "Merc 240D", "Merc 230", "Merc 280", "Merc 280C", "Merc 450SE", "Merc 450SL", "Merc 450SLC", "Cadillac Fleetwood", "Lincoln Continental", "Chrysler Imperial", "Fiat 128", "Honda Civic", "Toyota Corolla", "Toyota Corona", "Dodge Challenger", "AMC Javelin", "Camaro Z28", "Pontiac Firebird", "Fiat X1-9", "Porsche 914-2", "Lotus Europa", "Ford Pantera L", "Ferrari Dino", "Maserati Bora", "Volvo 142E"), class = "data.frame" I tried to do it in the loop but I don't know how to store the results. I get only the last value... for(z in 1:nrow(mtcars)){ vec_1 <- mtcars[z,1:7] vec_2 <- mtcars_mod[z,1:7] vec_results <- unlist(t.test(vec_1, vec_2)[3]) } Can someone show me how to correct my loop ? I would prefer to use apply function but still would like to know what I did wrong with my loop....
(I'll just use my own modified mtcarsmod ... sorry, yours is missing at least one paren, and -- though I know exactly what happened -- it is ugly in that SO window!) set.seed(42) mtcarsmod <- as.data.frame(lapply(mtcars, jitter, factor = 5)) head(mtcarsmod) # mpg cyl disp hp drat wt qsec vs am gear carb # 1 21.1 5.55 160 109.7 3.89 2.62 16.5 -0.373 0.221 3.68 3.861 # 2 21.1 6.74 160 110.0 3.90 2.88 17.0 0.641 1.080 3.06 3.788 # 3 22.8 2.02 108 93.5 3.86 2.32 18.6 0.614 1.142 4.73 0.284 # 4 21.5 7.33 258 110.2 3.08 3.21 19.4 0.371 0.238 3.46 0.560 # 5 18.7 6.03 360 175.3 3.15 3.44 17.0 -0.903 0.430 2.63 2.130 # 6 18.1 4.83 225 104.4 2.77 3.46 20.2 0.491 -0.753 2.77 1.870 Instead of a loop you should probably use sapply or one of its kin. sapply(seq_len(nrow(mtcars)), function(r) unlist(t.test(mtcars[r,1:7], mtcarsmod[r,1:7])[3])) # p.value p.value p.value p.value p.value p.value p.value p.value p.value p.value p.value # 0.998 0.998 0.992 0.996 0.998 0.995 0.999 1.000 0.999 0.998 0.995 # p.value p.value p.value p.value p.value p.value p.value p.value p.value p.value p.value # 0.995 0.999 0.999 0.998 0.999 0.997 0.999 0.995 0.997 0.995 0.999 # p.value p.value p.value p.value p.value p.value p.value p.value p.value p.value # 0.997 0.998 1.000 0.990 0.997 0.999 0.999 0.995 0.997 0.995 One advantage to using lapply might be using more of the test results. For instance: ret <- lapply(seq_len(nrow(mtcars)), function(r) t.test(mtcars[r,1:7], mtcarsmod[r,1:7])) str(head(ret, n = 2)) # List of 2 # $ :List of 9 # ..$ statistic : Named num 0.0024 # .. ..- attr(*, "names")= chr "t" # ..$ parameter : Named num 12 # .. ..- attr(*, "names")= chr "df" # ..$ p.value : num 0.998 # ..$ conf.int : atomic [1:2] -73.4 73.5 # .. ..- attr(*, "conf.level")= num 0.95 # ..$ estimate : Named num [1:2] 45.7 45.6 # .. ..- attr(*, "names")= chr [1:2] "mean of x" "mean of y" # ..$ null.value : Named num 0 # .. ..- attr(*, "names")= chr "difference in means" # ..$ alternative: chr "two.sided" # ..$ method : chr "Welch Two Sample t-test" # ..$ data.name : chr "mtcars[r, 1:7] and mtcarsmod[r, 1:7]" # ..- attr(*, "class")= chr "htest" # $ :List of 9 # ..$ statistic : Named num -0.00311 # .. ..- attr(*, "names")= chr "t" # ..$ parameter : Named num 12 # .. ..- attr(*, "names")= chr "df" # ..$ p.value : num 0.998 # ..$ conf.int : atomic [1:2] -73.4 73.2 # .. ..- attr(*, "conf.level")= num 0.95 # ..$ estimate : Named num [1:2] 45.8 45.9 # .. ..- attr(*, "names")= chr [1:2] "mean of x" "mean of y" # ..$ null.value : Named num 0 # .. ..- attr(*, "names")= chr "difference in means" # ..$ alternative: chr "two.sided" # ..$ method : chr "Welch Two Sample t-test" # ..$ data.name : chr "mtcars[r, 1:7] and mtcarsmod[r, 1:7]" # ..- attr(*, "class")= chr "htest" ret[[1]]$p.value # [1] 0.998 And you can still easily get a vector of p-values from the results: sapply(ret, `[[`, "p.value") # [1] 0.998 0.998 0.992 0.996 0.998 0.995 0.999 1.000 0.999 0.998 0.995 0.995 0.999 0.999 # [15] 0.998 0.999 0.997 0.999 0.995 0.997 0.995 0.999 0.997 0.998 1.000 0.990 0.997 0.999 # [29] 0.999 0.995 0.997 0.995