我正在尝试更改列表的多个特定列。我想使某些行(特别是2,3,5,6)中的所有值都相同。即,列中的值对于每一行是相同的。我想将第4和第7列从NA'更改为零(0)
我可以使用以下内容向列表中的每个数据框添加一个带零的列:
lapply(df1, function(x) cbind(x,replace(x$efficiency, is.na(x$efficiency), "0")))
我一直试图用这个:
lapply(df1, na.locf,df1$Receiver)
作为一种解决方法,但进展甚微
示例数据:
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"))
期望的输出:
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"))
答案 0 :(得分:3)
我不太确定你想做什么,但这应该有所帮助。创建一个修复单个数据框的函数
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)
}
测试:
fix_data_frame(df[[1]])
fix_data_frame(df[[2]])
一旦有效,只需将其贴在lapply
lapply(df1, fix_data_frame)