Looping through subjects and creating a matrix for each subject

Hey all,

I have the following code:

rachelsdata <- read.csv("yourcsvfilehere")

participant116 = matrix(nrow = 80, ncol = 5)

for (i in 1:nrow(rachelsdata)) {

if (rachelsdata[i,1] == 116){

participant116[i,] = rachelsdata[i,]

}
}
  1. I want to create an empty matrix for each participant ID (which is in the first column of the CSV file
  2. I then want to loop through each row of the CSV file
  3. If that particular row contains the ID of a particular participant, in this case 116, the data from that row is placed into the empty matrix for that participant

Would anybody be able to help me amend my code to allow me to do this?

I would be so grateful!

Below is a snippet of the raw data from the CSV file:

Hi @eyavuz21,

What you're suggesting is possible, but unless there is a compelling reason not to load the whole data into R and then process it there, it's not a particularly efficient process.

Using the gapminder data as example (lets pretend country name is similar to your Participant column. We can load the whole data set into R, then split the data into separate data frames based on country, and then turn them all into matrices.

library(gapminder)
library(magrittr)

gapminder
#> # A tibble: 1,704 x 6
#>    country     continent  year lifeExp      pop gdpPercap
#>    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
#>  1 Afghanistan Asia       1952    28.8  8425333      779.
#>  2 Afghanistan Asia       1957    30.3  9240934      821.
#>  3 Afghanistan Asia       1962    32.0 10267083      853.
#>  4 Afghanistan Asia       1967    34.0 11537966      836.
#>  5 Afghanistan Asia       1972    36.1 13079460      740.
#>  6 Afghanistan Asia       1977    38.4 14880372      786.
#>  7 Afghanistan Asia       1982    39.9 12881816      978.
#>  8 Afghanistan Asia       1987    40.8 13867957      852.
#>  9 Afghanistan Asia       1992    41.7 16317921      649.
#> 10 Afghanistan Asia       1997    41.8 22227415      635.
#> # … with 1,694 more rows

country_matrix <- 
  gapminder %>%
  split(gapminder$country) %>% 
  lapply(as.matrix)

# We can access a specific country by name or number
country_matrix[["Canada"]]
#>       country  continent  year   lifeExp  pop        gdpPercap 
#>  [1,] "Canada" "Americas" "1952" "68.750" "14785584" "11367.16"
#>  [2,] "Canada" "Americas" "1957" "69.960" "17010154" "12489.95"
#>  [3,] "Canada" "Americas" "1962" "71.300" "18985849" "13462.49"
#>  [4,] "Canada" "Americas" "1967" "72.130" "20819767" "16076.59"
#>  [5,] "Canada" "Americas" "1972" "72.880" "22284500" "18970.57"
#>  [6,] "Canada" "Americas" "1977" "74.210" "23796400" "22090.88"
#>  [7,] "Canada" "Americas" "1982" "75.760" "25201900" "22898.79"
#>  [8,] "Canada" "Americas" "1987" "76.860" "26549700" "26626.52"
#>  [9,] "Canada" "Americas" "1992" "77.950" "28523502" "26342.88"
#> [10,] "Canada" "Americas" "1997" "78.610" "30305843" "28954.93"
#> [11,] "Canada" "Americas" "2002" "79.770" "31902268" "33328.97"
#> [12,] "Canada" "Americas" "2007" "80.653" "33390141" "36319.24"
1 Like

Hey @mattwarkentin,

Thank you so much for your help!

I did it this way, using the split function:

out <- split(rachelsdata, f = rachelsdata$Participant)
participant116 = as.data.frame(out[1])

:slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.