簡體   English   中英

將具有相同值的列變量組合到新變量中

[英]combining column variables with same values into a new variable

正在使用的數據集在此Google表格鏈接中https://docs.google.com/spreadsheets/d/1eV33Sgx_UVtk2vDtNBc4Yqs_kQoeffY0oj5gSCq9rCs/edit?usp=sharing

AMC.dataset$ExamMC.A<-surveySP15$Exams_A
AMC.dataset$ExamMC.A<-factor(NA, levels=c("TRUE", "FALSE"))
AMC.dataset$ExamMC.A[AMC.dataset$Exams_A=="1 time"|AMC.dataset$Exams_A=="2-4 times"|AMC.dataset$Exams_A==">4 times"]<-"TRUE"
AMC.dataset$ExamMC.A[AMC.dataset$Exams_A=="0 times"]<-"FALSE"
AMC.dataset$ExamMC.A=as.logical(AMC.dataset$ExamMC.A)

對於那些對這9個變量中的任何一個回答1次或更多次的人,我使用這5行代碼將所有9個Exams_A到Exams_I變量重新編碼為“ True”的邏輯二進制結果。 我想將所有這些變量組合到數據集中的新列中,對於每個觀察行,如果在整個行中甚至對於9個體檢A到I中的任何一個都為“真”的情況,則該新變量結果將顯示為“ true”,表示他們至少一次犯了記錄在數據集中的9種類型的任何考試學術不端行為。 如果觀察行中沒有真實結果,我希望新的可變結果讀為“假”,表示他們(觀察行)從未犯過考試學術不端行為

我對這個新變量的代碼的理解是

 AMC.dataset$ExamMC = any(AMC.dataset$ExamMC.A, AMC.dataset$ExamMC.B, AMC.dataset$ExamMC.C, AMC.dataset$ExamMC.D, AMC.dataset$ExamMC.E, AMC.dataset$ExamMC.F, AMC.dataset$ExamMC.G, AMC.dataset$ExamMC.H, AMC.dataset$ExamMC.I)

但是,此代碼已被字符串中的最后一個變量輸出(AMC.dataset $ ExamMC.I)覆蓋,該變量具有215個False情況和0 true,它將覆蓋字符串的其余部分以提供新的變量輸出215“ false即使其他變量可能將“ True”作為其案例輸出,也是如此。

編輯

我現在為檢查失當行為變量集創建了一個數據框

AMC.dataset$ExamMCdf<-data.frame(AMC.dataset$ExamMC.A, AMC.dataset$ExamMC.B, AMC.dataset$ExamMC.C, AMC.dataset$ExamMC.D, AMC.dataset$ExamMC.E, AMC.dataset$ExamMC.F, AMC.dataset$ExamMC.G, AMC.dataset$ExamMC.H, AMC.dataset$ExamMC.I)

現在我的問題是如何在新列中創建一個正確讀取每個觀察行的復合變量,並將在數據框中甚至具有單個“ true”結果的任何行標記為復合變量的“ true”。 任何沒有“ true”結果的觀察行都應由復合變量標記為“ false”。

感謝您所有的幫助。

我不確定100%會追隨您,但是我將按照我想的方式執行以下操作:

library(data.table)
setDT(surveySP15)

exams <- paste0("Exams_", LETTERS[1:9])
surveySP15[ , paste0(exams, "_binary") :=
             lapply(.SD, function(x) x %in% c("1 time", "2-4 times", ">4 times")),
           .SDcols = exams]

這將為每個檢查創建一個變量(例如Exams_A_binary ),如果該變量在數據中的編碼時間為1次或以上,則該變量為( logicalTRUE否則為FALSE 這是相關的輸出:

> surveySP15[ , paste0(exams, "_binary"), with = FALSE]
     Exams_A_binary Exams_B_binary Exams_C_binary Exams_D_binary Exams_E_binary Exams_F_binary Exams_G_binary
  1:          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE
  2:          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE
  3:          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE
  4:          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE
  5:          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE
 ---                                                                                                         
223:          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE
224:           TRUE           TRUE           TRUE          FALSE           TRUE          FALSE          FALSE
225:          FALSE           TRUE          FALSE          FALSE          FALSE          FALSE          FALSE
226:          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE
227:          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE          FALSE
     Exams_H_binary Exams_I_binary
  1:          FALSE          FALSE
  2:          FALSE          FALSE
  3:          FALSE          FALSE
  4:          FALSE          FALSE
  5:          FALSE          FALSE
 ---                              
223:          FALSE          FALSE
224:          FALSE          FALSE
225:          FALSE          FALSE
226:          FALSE          FALSE
227:          FALSE          FALSE

要創建一個復合行以檢查其他數據框列中的任何TRUE值,請使用apply()包裝的any()函數逐行進行操作。 我認為您可以將其應用於您的情況:

#Makes a dataframe with TRUE/FALSE values and a low chance for TRUE
set.seed(123)
data <- data.frame(
  Exams_A = sample(c(TRUE,FALSE), 10, TRUE, c(.1, .9)),
  Exams_B = sample(c(TRUE,FALSE), 10, TRUE, c(.1, .9)),
  Exams_C = sample(c(TRUE,FALSE), 10, TRUE, c(.1, .9)),
  Exams_D = sample(c(TRUE,FALSE), 10, TRUE, c(.1, .9)),
  Exams_E = rep(TRUE,10) # Inserts row of all TRUE's to show that you can limit scope
)

data$ExamMC <- apply(data[, 1:4], 1, function(x) any(x))
data$ExamMC <- apply(data[, 1:4], 1, any) # This is the updated version
                          # ^ This part sets what columns you want to search

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM