bedingt umbenennen Ich habe eine Liste von 216 Datenrahmen mit jeweils 3 Variablen. Zum Beispiel:Datenrahmen Spalten in einer Liste abhängig von der Position in der Liste
Ich muss die Variablen in jedem Datenrahmen sequenziell umbenennen abhängig von seiner Position in der Liste. Das kann ich tun. Zum Beispiel:
#create lists of the variable names
Bnames <- c("feel1", "feel2")
Cnames <- c("cat1", "cat2")
#sequentially name each data frame's columns
k <- 0
for(i in 1:length(listDF)){
k = k+1
names(listDF[[i]]) <- c("ID",Bnames[k],Cnames[k])
}
#I know people prefer lapply; I tend to switch back and forth depending on what I'm doing
Das Problem, das ich habe, ist, dass in der Liste der 216 Datenrahmen (24 „cat“ Variablen x 9 „Gefühl“ Variablen = 216), muss ich die Listen der ‚bNames‘ und 'CNames', um mit unterschiedlichen Raten zu sequenzieren. Ich brauche die ersten 9 Datenrahmen, um C = cat1 zu haben, mit B = Gefühl1: 9, die nächsten 9, um C = cat2 zu haben, mit B = Gefühl1: 9 und so weiter. Also muss ich rekursiv entlang B ablaufen, aber alle 9 Datenrahmen langsam entlang C ablaufen lassen. 'A' sollte in jedem Datenrahmen "ID" sein.
Ich bin wirklich ratlos wie das geht. Vielen Dank im Voraus für Anregungen!
Auch - wenn jemand einen Vorschlag für einen verständlicheren Titel hat, werde ich es gerne ändern.
EDIT:
Es könnte helfen wissen, wo ich am Ende will, wenn ich fertig bin. Jede ID ist in einer unterschiedlichen Anzahl des Datenrahmen, und letztlich das, was ich will, ist neu zu gestalten und den Datenrahmen in 1 Datenrahmen mit folgendem Format zu verschmelzen:
ID feel1.1 feel1.2 ... feel2.1 feel2.2
2 NA 4 NA 7
3 2 1 6 3
Wo feel1.1 den „feel1“ zeigt Wert für "cat1" und es fehlen Werte, wenn eine ID keine bestimmte Kombination von "feel" und "cat" hatte (also hatte ID 2 keinen feel1-Wert für cat1, aber für cat2. Letztendlich sollte es 217 sein Spalten und so viele Zeilen wie es IDs sind
Meine (sehr schlecht) Lösung:.
X <- listDF
#create lists of the data frame numbers for each "feel" variable
feel1 <- seq(1,216,by=9)
feel2 <- seq(2,216,by=9)
feel3 <- seq(3,216,by=9)
feel4 <- seq(4,216,by=9)
feel5 <- seq(5,216,by=9)
feel6 <- seq(6,216,by=9)
feel7 <- seq(7,216,by=9)
feel8 <- seq(8,216,by=9)
feel9 <- seq(9,216,by=9)
#assign correct names for the "feel" variables in each data frame
for(i in 1:length(X)){
if(i %in% feel1){
names(X[[i]]) <- c("UniqueID", "cat", "feel1")
}
if(i %in% feel2){
names(X[[i]]) <- c("UniqueID", "cat", "feel2")
}
if(i %in% feel3){
names(X[[i]]) <- c("UniqueID", "cat", "feel3")
}
if(i %in% feel4){
names(X[[i]]) <- c("UniqueID", "cat", "feel4")
}
if(i %in% feel5){
names(X[[i]]) <- c("UniqueID", "cat", "feel5")
}
if(i %in% feel6){
names(X[[i]]) <- c("UniqueID", "cat", "feel6")
}
if(i %in% feel7){
names(X[[i]]) <- c("UniqueID", "cat", "feel7")
}
if(i %in% feel8){
names(X[[i]]) <- c("UniqueID", "cat", "feel8")
}
if(i %in% feel9){
names(X[[i]]) <- c("UniqueID", "cat", "feel9")
}
}
#'melt' each of the dataframes and then remove the rows with 'cat'
X <- lapply(X, function(x) melt(x, id.vars ="UniqueID"))
X <- lapply(X, function(x) subset(x, variable != "cat"))
#add the appropriate 'cat' number to each 'feel' name
for(j in 1:length(X)){
if(j <= 9){
X[[j]]$variable <- paste0(X[[j]]$variable, ".24")
}
if(j > 9 & j <= 18){
X[[j]]$variable <- paste0(X[[j]]$variable, ".1")
}
if(j > 18 & j <= 27){
X[[j]]$variable <- paste0(X[[j]]$variable, ".2")
}
if(j > 27 & j <= 36){
X[[j]]$variable <- paste0(X[[j]]$variable, ".3")
}
if(j > 36 & j <= 45){
X[[j]]$variable <- paste0(X[[j]]$variable, ".4")
}
if(j > 45 & j <= 54){
X[[j]]$variable <- paste0(X[[j]]$variable, ".5")
}
if(j > 54 & j <= 63){
X[[j]]$variable <- paste0(X[[j]]$variable, ".6")
}
if(j > 63 & j <= 72){
X[[j]]$variable <- paste0(X[[j]]$variable, ".7")
}
if(j > 72 & j <= 81){
X[[j]]$variable <- paste0(X[[j]]$variable, ".8")
}
if(j > 81 & j <= 90){
X[[j]]$variable <- paste0(X[[j]]$variable, ".9")
}
if(j > 90 & j <= 99){
X[[j]]$variable <- paste0(X[[j]]$variable, ".10")
}
if(j > 99 & j <= 108){
X[[j]]$variable <- paste0(X[[j]]$variable, ".11")
}
if(j > 108 & j <= 117){
X[[j]]$variable <- paste0(X[[j]]$variable, ".12")
}
if(j > 117 & j <= 126){
X[[j]]$variable <- paste0(X[[j]]$variable, ".13")
}
if(j > 126 & j <= 135){
X[[j]]$variable <- paste0(X[[j]]$variable, ".14")
}
if(j > 135 & j <= 144){
X[[j]]$variable <- paste0(X[[j]]$variable, ".15")
}
if(j > 144 & j <= 153){
X[[j]]$variable <- paste0(X[[j]]$variable, ".16")
}
if(j > 153 & j <= 162){
X[[j]]$variable <- paste0(X[[j]]$variable, ".17")
}
if(j > 162 & j <= 171){
X[[j]]$variable <- paste0(X[[j]]$variable, ".18")
}
if(j > 171 & j <= 180){
X[[j]]$variable <- paste0(X[[j]]$variable, ".19")
}
if(j > 180 & j <= 189){
X[[j]]$variable <- paste0(X[[j]]$variable, ".20")
}
if(j > 189 & j <= 198){
X[[j]]$variable <- paste0(X[[j]]$variable, ".21")
}
if(j > 198 & j <= 207){
X[[j]]$variable <- paste0(X[[j]]$variable, ".22")
}
if(j > 207 & j <= 216){
X[[j]]$variable <- paste0(X[[j]]$variable, ".23")
}
}
#reshape each data frame into 2 columns: ID and the renamed 'feel' variable
X <- lapply(X, function(x) dcast(x, UniqueID ~ variable))
#merge it back onto the original dataset
for(i in 1:length(X)){
data <- merge(data, X[[i]], by="UniqueID", all=T)
}
Sie können 'Bibliothek (data.table); rbindlist (dfList, idcol = TRUE) ', die Ihre Daten stapeln und eine Spalte namens' .id' basierend auf der Reihenfolge in der Liste erstellen. Von dort können Sie auf '.id' konditionieren (und vielleicht müssen Sie nichts umbenennen). – Frank