Trying to store data from these 2 loops into a DF

Hello all,

I am in need of some emergency help. I am working on an assignment that is due tuesday, i am 30 hrs deep and i have a few problems.

One of my smaller problems but bigger ones is I have these 2 loops, and I want to store the outputs into a data dataframe, but when I run it only the last entry is being stored and none of the others.

Any help would be greatly appreciated.



class_roster <- data.frame(read.csv("class_roster.csv"))
print(class_roster)
class_roster <- tibble::rowid_to_column(class_roster,"Student_ID")

class_roster <- class_roster %>% unite('Full_Name', First.Name:Last.Name, remove = FALSE) 
numbers_used_Q3 <- 1:nrow(class_roster)
print(class_roster)
print(numbers_used_Q3)




student_marking <- vector() #who is marking
#students_used <- vector() #people being marked by Student_marking
NQ3 <- 33
final_list <- data.frame()

if(NQ3 < 4)
{

print("This roster size is too small. Minimum of 4")

}else
{

###################################################################
#Go over the whole class list
#for (i in 1:NQ3)
for (i in 1: 6){

    #Get the student who is going to be the marker
    if(i <= 6) #NQ3
    {
      temp_list <- c()
     
      #Make sure no duplicate student markers are used 
      while (TRUE) {
        
        rng_Q3 <- sample(numbers_used_Q3, 1) #pick a student to be the marker
       
        print(paste("Student who is marking is " , rng_Q3))
        
        
        if(!rng_Q3 %in% student_marking) #check for previously used student markers
        {
          
         break
        }#end IF
        
        
        } #end while
      student_marking <- append(student_marking, rng_Q3) #This stores the list of students already picked to be markers
      
   ################################################   
      students_used <- vector()
       for (i  in 1:3)
  {
         
        
    while (TRUE)
    {
      rng_Q3_1 <- sample(numbers_used_Q3, 1)
      print(paste("Students projects to be graded ", rng_Q3_1))
      
      
     if(rng_Q3_1 != rng_Q3) #make sure the project being picked is the markers project
     {
       break
       
       
       #end IF
     }
      
      if(!rng_Q3_1 %in% students_used) #check to make sure the same student project isnt being picked twice for the marker
      {
        
        break
        
      } #end IF
      
     temp_list <- c(temp_list, rng_Q3, rng_Q3_1)
     print("temp list")
     print(temp_list)
     
      }#END WHILE
         
   
         students_used <- append(students_used, rng_Q3_1)
 
    
    
  } #END FOR LOOP 1:3
     
      
    } #end IF
    

    } #End For
  
}#end Else

See the FAQ: How to do a minimal reproducible example reprex for beginners. Without representative data only general answers can be given.

The way to think about functions in R is like the old commercial

What happens in Las Vegas stays in Las Vegas

In the for loop temp_list is getting clobbered each iteration, and only the last through the gate survives to tell the tale. To escape everything, each iteration must be saved to an object that will persist when the function finishes.

The most straightforward way to do this is to pre-allocate a receiver object in the global environment. Here's a silly example

holder <- vector(mode = "character", length = 26)
for(i in seq_along(LETTERS)) holder[i] = tolower(LETTERS[i])
holder
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"

(Silly, because the way to do this if we just wanted to lowercase the uppercase letters would be tolower(LETTERS).)

Think of this as school algebra f(x) = y. x is what is to hand, y is what is required and f is a function, which may be composite to transform one to the other.

In the example x is an object containing the uppercase Roman letters and y contains the corresponding lowercase. As we want to create y piecewise, we create holder as a receiver object to save intermediate results each time through the loop. Then f is a composite of for seq_along and tolower, along with the subset operator [.

for makes it piecewise, seq_along allows sizing the number of iterations to the size of the object to be iterated over, instead of in 1:26 (always better not to hardwire) and tolower does the transformation of upper to lower on each element.

If this makes sense, apply f(x) to your data.

This topic was automatically closed 42 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.