Skip to contents

Delete cases from specific sessions in all data frames in the oTree list of data frames.

Caution 1: This function does not delete cases from the original CSV and Excel files!

Caution 2: This function does not delete cases from custom exports if the custom exports do not have a variable named participant.code and a variable named session.code!

Usage

delete_sessions(oTree, scodes, saved_vars = NULL, reason, info = FALSE)

Arguments

oTree

A list of data frames that were created by import_otree.

scodes

Character. The session.code(s) of the session(s) whose data should be removed.

saved_vars

Character. The name(s) of variable(s) that need(s) to be stored in the list of information on deleted cases in $info$deleted_cases.

reason

Character. The reason for deletion that should be stored in the list of information on deleted cases in $info$deleted_cases.

info

Logical. TRUE if a brief information on the session deletion process should be printed.

Value

This function returns a duplicate of the original oTree list of data frames that do not include the deleted sessions.

It adds information on the deleted cases to $info$deleted_cases. (This list is also filled by other functions.)

In this list, you can find the following information:

- $full and $unique = The data frames $full and $unique contain information on all participants whose data were deleted. The entries to the $full and the $unique data frames in this list are the same. Columns end_app and end_page are left empty intentionally because they are only filled by the delete_dropouts function. Columns participant.code and reason are filled.

- $codes = A vector containing the participant codes of all deleted participants.

- $count = The number of all deleted participants.

Examples

# Use package-internal list of oTree data frames
oTree <- gmoTree::oTree

# Delete one session
oTree2 <- delete_sessions(oTree,
  scodes = "7bfqtokx",
  reason = "Only tests")

# Show changes in row numbers
print(paste("Row numbers before deletion: ", nrow(oTree$all_apps_wide), nrow(oTree$survey),
nrow(oTree$Time), nrow(oTree$Chats)))
#> [1] "Row numbers before deletion:  29 25 289 35"

print(paste("Row numbers after deletion: ", nrow(oTree2$all_apps_wide), nrow(oTree2$survey),
nrow(oTree2$Time), nrow(oTree2$Chats)))
#> [1] "Row numbers after deletion:  20 16 166 14"

# Delete two sessions and show deletion message
oTree2 <- delete_sessions(oTree,
  scodes = c("7bfqtokx", "vd1h01iv"),
  reason = "Only tests",
  info = TRUE)
#> 13 case(s) deleted

# Show row numbers again
print(paste(nrow(oTree2$all_apps_wide), nrow(oTree2$survey),
nrow(oTree2$Time), nrow(oTree2$Chats)))
#> [1] "16 12 126 10"

# Delete session and save variable to the info list
oTree2 <- delete_sessions(oTree,
  scodes = c("7bfqtokx", "vd1h01iv"),
  reason = "Server Crash",
  saved_vars = "dictator.1.group.id_in_subsession")

# Check the "full" deletion information
oTree2$info$deleted_cases$full
#>    participant.code session.code dictator.1.group.id_in_subsession end_app
#> 1          46kxib6w     7bfqtokx                                 1        
#> 2          iay3dhkn     7bfqtokx                                 1        
#> 3          4zhzdmzo     7bfqtokx                                 2        
#> 4          3ttf7yix     7bfqtokx                                 2        
#> 5          2d72mfgh     7bfqtokx                                 3        
#> 6          rvce7958     7bfqtokx                                 3        
#> 7          164r1hs4     7bfqtokx                                 4        
#> 8          lsl3vbij     7bfqtokx                                 4        
#> 9          ktjz5jli     7bfqtokx                                 5        
#> 11         xx78b3x0     vd1h01iv                                 1        
#> 12         xmxl46rm     vd1h01iv                                 1        
#> 13         vbhvhozv     vd1h01iv                                 2        
#> 14         2scvem7a     vd1h01iv                                 2        
#>    end_page       reason
#> 1           Server Crash
#> 2           Server Crash
#> 3           Server Crash
#> 4           Server Crash
#> 5           Server Crash
#> 6           Server Crash
#> 7           Server Crash
#> 8           Server Crash
#> 9           Server Crash
#> 11          Server Crash
#> 12          Server Crash
#> 13          Server Crash
#> 14          Server Crash

# See codes of deleted variables
oTree2$info$deleted_cases$codes
#>  [1] "46kxib6w" "iay3dhkn" "4zhzdmzo" "3ttf7yix" "2d72mfgh" "rvce7958"
#>  [7] "164r1hs4" "lsl3vbij" "ktjz5jli" "xx78b3x0" "xmxl46rm" "vbhvhozv"
#> [13] "2scvem7a"

# See number of deleted variables
oTree2$info$deleted_cases$count
#> [1] 13

# Delete a single case and then delete a session
oTree2 <- delete_cases(oTree,
                       pcodes = "4zhzdmzo",
                       reason = "requested")
oTree2 <- delete_sessions(oTree2,
  scodes = c("vd1h01iv"),
  reason = "Server Crash",
  saved_vars = "dictator.1.group.id_in_subsession")

# Check the "full" deletion information
oTree2$info$deleted_cases$full
#>   participant.code  session end_app end_page       reason session.code
#> 1         4zhzdmzo 7bfqtokx                     requested         <NA>
#> 2         xx78b3x0     <NA>                  Server Crash     vd1h01iv
#> 3         xmxl46rm     <NA>                  Server Crash     vd1h01iv
#> 4         vbhvhozv     <NA>                  Server Crash     vd1h01iv
#> 5         2scvem7a     <NA>                  Server Crash     vd1h01iv
#>   dictator.1.group.id_in_subsession
#> 1                                NA
#> 2                                 1
#> 3                                 1
#> 4                                 2
#> 5                                 2

# See codes of deleted variables
oTree2$info$deleted_cases$codes
#> [1] "xx78b3x0" "xmxl46rm" "vbhvhozv" "2scvem7a" "4zhzdmzo"

# See number of deleted variables
oTree2$info$deleted_cases$count
#> [1] 5