我正在清理excel文件,任务之一是使列名正确。在此过程中,我得到了某些列名的不适用。在所有这些情况下,我都想填写该特定列的第一行的值。
一个最小的示例如下:
structure(list(c(NA, "1"), c(NA, "FOB"), c(NA, "WEH, Nr. Oberoi"
), c("W", "60"), c("H", "10"), c(NA, "1"), c(NA, "600"), c(NA,
"01.01.19"), c(NA, "31.01.19"), c(NA, "30"), c("Vendor", "Capital"
), c("Display Cost", "900000"), c(8L, 0L)), .Names = c("Sr. No.",
"Media Vehicle", "Location", "Size", NA, "Qty", "Area", "Start Date",
"End Date", "Duration", "CTU", NA, "2"), row.names = 4:5, class = "data.frame")
所需的输出如下:
structure(list(c(NA, "1"), c(NA, "FOB"), c(NA, "WEH, Nr. Oberoi"
), c("W", "60"), c("H", "10"), c(NA, "1"), c(NA, "600"), c(NA,
"01.01.19"), c(NA, "31.01.19"), c(NA, "30"), c("Vendor", "Capital"
), c("Display Cost", "900000"), c(8L, 0L)), .Names = c("Sr. No.",
"Media Vehicle", "Location", "Size", "H", NA, "Area", "Start Date",
"End Date", "Duration", "CTU", "Display Cost", "2"), row.names = 4:5, class = "data.frame")
我尝试使用if条件,但是由于名称中包含三个NA,因此不确定是否必须使用for循环遍历每个na值。
if (is.na(names(dat))) {
b <- which(is.na(colnames(dat)))
names(dat)[b][1] <- dat[1,b]
}
答案 0 :(得分:1)
我们首先可以在名称中找到NA
索引,然后使用这些索引从数据帧的第一行中分配子集并分配名称。
inds <- is.na(names(df))
names(df)[inds] <- df[1, inds]
df
# Sr. No. Media Vehicle Location Size H Qty Area Start Date End Date Duration CTU Display Cost 2
#4 <NA> <NA> <NA> W H <NA> <NA> <NA> <NA> <NA> Vendor Display Cost 8
#5 1 FOB WEH, Nr. Oberoi 60 10 1 600 01.01.19 31.01.19 30 Capital 900000 0