2016-04-22 7 views
0

Ich versuche, mehrere spezifische Spalten einer Liste zu ändern. Ich möchte alle Werte in einigen Zeilen (speziell 2,3,5,6) den gleichen Wert machen. h. der Wert innerhalb der Spalte ist für jede Zeile gleich. Ich möchte Spalten 4 und 7 von NA ändern, um Nullen (0)lapply auf Liste von Datenrahmen in bestimmten Spalten

Ich bin in der Lage eine Spalte mit Nullen zu jedem Datenrahmen in der Liste mit diesem hinzuzufügen:

lapply(df1, function(x) cbind(x,replace(x$efficiency, is.na(x$efficiency), "0"))) 

Ich habe versucht zu verwenden diese:

lapply(df1, na.locf,df1$Receiver) 

als Arbeit um, aber mit wenig Fortschritt

Beispieldaten:

df1<-list(structure(list(dt15 = structure(c(1457929800, 1457930700, 
1457931600, 1457932500, 1457933400, 1457934300), class = c("POSIXct", 
"POSIXt"), tzone = ""), Receiver = c(480432, 480432, NA, NA, 
NA, NA), Code = c(62431, 62431, NA, NA, NA, NA), detections = c(6, 
1, NA, NA, NA, NA), distance = c(168.948559873358, 168.948559873358, 
NA, NA, NA, NA), Repeat_Rate = c(90L, 90L, NA, NA, NA, NA), efficiency =  c("60", 
"10", NA, NA, NA, NA)), .Names = c("dt15", "Receiver", "Code", 
"detections", "distance", "Repeat_Rate", "efficiency"), row.names = 635:640,  class = "data.frame"), 
structure(list(dt15 = structure(c(1457956800, 1457957700, 
1457958600, 1457959500, 1457960400, 1457961300, 1457962200, 
1457963100), class = c("POSIXct", "POSIXt"), tzone = ""), 
    Receiver = c(480422, 480422, NA, NA, NA, NA, 480422, 
    480422), Code = c(62427, 62427, NA, NA, NA, NA, 62427, 
    62427), detections = c(2, 2, NA, NA, NA, NA, 1, 3), distance = c(301.8128773339, 
    301.8128773339, NA, NA, NA, NA, 301.8128773339, 301.8128773339 
    ), Repeat_Rate = c(90L, 90L, NA, NA, NA, NA, 90L, 90L 
    ), efficiency = c("20", "20", NA, NA, NA, NA, "10", "30" 
    )), .Names = c("dt15", "Receiver", "Code", "detections", 
    "distance", "Repeat_Rate", "efficiency"), row.names = 665:672, class = "data.frame")) 

gewünschte Ausgabe:

list(structure(list(dt15 = structure(c(1457929800, 1457930700, 
1457931600, 1457932500, 1457933400, 1457934300), class = c("POSIXct", 
"POSIXt"), tzone = ""), Receiver = c(480432, 480432, 480432, 
480432, 480432, 480432), Code = c(62431, 62431, 62431, 62431, 
62431, 62431), detections = c(6, 1, 0, 0, 0, 0), distance = c(168.948559873358, 
168.948559873358, 168.948559873358, 168.948559873358, 168.948559873358, 
168.948559873358), Repeat_Rate = c(90L, 90L, 90L, 90L, 90L, 90L 
), efficiency = c("60", "10", "0", "0", "0", "0")), .Names = c("dt15", 
"Receiver", "Code", "detections", "distance", "Repeat_Rate", 
"efficiency"), row.names = 635:640, class = "data.frame"), structure(list(
dt15 = structure(c(1457956800, 1457957700, 1457958600, 1457959500, 
1457960400, 1457961300, 1457962200, 1457963100), class = c("POSIXct", 
"POSIXt"), tzone = ""), Receiver = c(480422, 480422, 480422, 
480422, 480422, 480422, 480422, 480422), Code = c(62427, 
62427, 62427, 62427, 62427, 62427, 62427, 62427), detections = c(2, 
2, 0, 0, 0, 0, 1, 3), distance = c(301.8128773339, 301.8128773339, 
301.8128773339, 301.8128773339, 301.8128773339, 301.8128773339, 
301.8128773339, 301.8128773339), Repeat_Rate = c(90L, 90L, 
90L, 90L, 90L, 90L, 90L, 90L), efficiency = c("20", "20", 
"0", "0", "0", "0", "10", "30")), .Names = c("dt15", "Receiver", 
"Code", "detections", "distance", "Repeat_Rate", "efficiency" 
), row.names = 665:672, class = "data.frame")) 

Antwort

2

Ich bin mir nicht ganz sicher, was Sie tun wollen, aber das sollte helfen. Erstellen Sie eine Funktion, die einen einzelnen Datenrahmen fixiert

fix_data_frame = function(x) { 
    x[is.na(x[,7]),7] = 0 
    x[is.na(x[,4]),4] = 0 
    # What ever else you want to do 

    return(x) 
} 

Zum Test:

fix_data_frame(df[[1]]) 
fix_data_frame(df[[2]]) 

Sobald das funktioniert, halten Sie es einfach in lapply

lapply(df1, fix_data_frame)