Salmo salar
Salmo salar

Reputation: 557

lapply on list of data frames in specific columns

I am trying to change multiple specific columns of a list. I would like to make all values in some rows (specifically 2,3,5,6) the same value. i.e. the value within the column is the same for each row. I would like to change columns 4 and 7 from NA's to zero's (0)

I am able to add a column with zeros to each dataframe in the list with this:

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

I have been attempting to use this:

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

as a work around but with little progress

Sample Data:

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"))

Desired output:

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"))

Upvotes: 1

Views: 855

Answers (1)

csgillespie
csgillespie

Reputation: 60452

I'm not quite sure what you want to do, but this should help. Create a function that fixes a single data frame

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)
}

To test:

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

Once that works, just stick it in lapply

lapply(df1, fix_data_frame)

Upvotes: 3

Related Questions