根据相应列

时间:2015-07-16 18:29:25

标签: r indexing dataframe

两个请求。我尝试在线研究如何基于索引删除单元格,但没有发现任何内容。如果有人能指导我关于这个主题的资源(这是下面问题的解决方案),我将不胜感激。

我的问题是根据相应的trimmed.average列删除Date单元格。如果TRIMMED.AVERAGE列为空""或者是" TRIMMED AVERAGE",应该删除右边的日期。

我找到了删除整行的答案,但我希望将变量保留在该行中。

structure(list(Variable = c("X", "X", "X", "X", "X", "X", "X", 
"X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", 
"X", "X", "X", "X", "X", "X", "X", "X", "X"), X1ST = c("", "3.44", 
"0.53", "1.66", "0.10", "0.91", "-0.25", "0.16", "-0.11", "0.16", 
"0.09", "-0.19", "0.00", "0.09", "0.09", "", "81.00", "52.75", 
"3.66", "0.58", "1ST", "0.42", "0.05", "0.29", "0.91", "-2.12", 
"", "23.14", "32.78"), X5TH = c("", "4.48", "0.81", "2.85", "0.24", 
"1.63", "0.00", "0.71", "-0.02", "0.74", "0.49", "0.51", "0.00", 
"0.48", "0.49", "", "88.58", "63.76", "4.87", "0.89", "5TH", 
"0.81", "0.17", "0.43", "1.63", "0.88", "", "37.70", "38.55"), 
X10TH = c("", "4.73", "0.99", "3.10", "0.34", "1.93", "0.00", 
"0.98", "0.00", "1.00", "0.67", "0.70", "0.00", "0.66", "0.67", 
"", "90.05", "67.76", "5.11", "1.07", "10TH", "0.98", "0.21", 
"0.51", "1.93", "1.21", "", "44.33", "40.93"), X20TH = c("", 
"4.99", "1.21", "3.38", "0.50", "2.21", "0.03", "1.25", "0.00", 
"1.28", "0.84", "0.89", "0.00", "0.82", "0.84", "", "91.53", 
"72.93", "5.36", "1.30", "20TH", "1.17", "0.27", "0.64", 
"2.21", "1.53", "", "49.94", "44.32"), X25TH = c("", "5.09", 
"1.30", "3.46", "0.56", "2.33", "0.05", "1.34", "0.00", "1.35", 
"0.91", "0.95", "0.00", "0.88", "0.91", "", "91.95", "74.57", 
"5.44", "1.39", "25TH", "1.25", "0.29", "0.67", "2.33", "1.61", 
"", "52.74", "45.64"), X50TH = c("", "5.42", "1.59", "3.84", 
"0.81", "2.81", "0.13", "1.74", "0.00", "1.75", "1.17", "1.28", 
"0.00", "1.13", "1.17", "", "93.79", "79.50", "5.78", "1.71", 
"50TH", "1.53", "0.40", "0.84", "2.81", "1.91", "", "59.58", 
"52.20"), X75TH = c("", "5.77", "1.86", "4.28", "1.15", "3.36", 
"0.24", "2.15", "0.00", "2.18", "1.50", "1.62", "0.00", "1.40", 
"1.50", "", "95.44", "83.61", "6.17", "1.98", "75TH", "1.87", 
"0.50", "1.05", "3.36", "2.28", "", "66.90", "62.47"), X80TH = c("", 
"5.91", "1.93", "4.41", "1.24", "3.51", "0.27", "2.31", "0.00", 
"2.32", "1.62", "1.73", "0.00", "1.48", "1.62", "", "95.79", 
"84.68", "6.33", "2.06", "80TH", "1.97", "0.53", "1.11", 
"3.51", "2.40", "", "69.24", "64.82"), X90TH = c("", "6.22", 
"2.12", "4.74", "1.64", "3.98", "0.37", "2.66", "0.03", "2.71", 
"2.00", "2.11", "0.00", "1.73", "2.00", "", "96.77", "86.91", 
"6.69", "2.24", "90TH", "2.21", "0.63", "1.35", "3.98", "2.69", 
"", "74.61", "76.34"), X95TH = c("", "6.68", "2.25", "5.06", 
"2.30", "4.66", "0.49", "3.16", "0.08", "3.20", "2.36", "2.45", 
"0.00", "2.01", "2.36", "", "97.61", "88.15", "7.09", "2.38", 
"95TH", "2.62", "0.72", "1.59", "4.66", "2.93", "", "79.82", 
"88.33"), X99TH = c("", "8.57", "2.59", "6.76", "13.08", 
"12.74", "1.68", "5.81", "0.28", "6.74", "4.65", "5.14", 
"0.00", "4.13", "4.65", "", "99.18", "91.06", "9.23", "2.72", 
"99TH", "4.35", "1.03", "5.48", "12.74", "3.68", "", "93.81", 
"149.14"), TRIMMED.AVERAGE = c("", "5.45", "1.58", "3.88", 
"0.88", "2.86", "0.15", "1.77", "0.00", "1.79", "1.22", "1.31", 
"0.00", "1.15", "1.22", "", "93.64", "78.75", "5.83", "1.69", 
"TRIMMED AVERAGE", "1.57", "0.40", "0.87", "2.86", "1.93", 
"", "59.66", "54.70"), Date = c("2005-03-31", "2005-03-31", 
"2005-03-31", "2005-03-31", "2005-03-31", "2005-03-31", "2005-03-31", 
"2005-03-31", "2005-03-31", "2005-03-31", "2005-03-31", "2005-03-31", 
"2005-03-31", "2005-03-31", "2005-03-31", "2005-03-31", "2005-03-31", 
"2005-03-31", "2005-03-31", "2005-03-31", "2005-03-31", "2005-03-31", 
"2005-03-31", "2005-03-31", "2005-03-31", "2005-03-31", "2005-03-31", 
"2005-03-31", "2005-03-31")), .Names = c("Variable", "X1ST", 
"X5TH", "X10TH", "X20TH", "X25TH", "X50TH", "X75TH", "X80TH", 
"X90TH", "X95TH", "X99TH", "TRIMMED.AVERAGE", "Date"), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 
60L), class = "data.frame")        

1 个答案:

答案 0 :(得分:1)

我们可以使用%in%来获取“TRIMMED.AVERAGE”列中''TRIMMED AVERAGE元素的索引,并使用它来指定“日期”列值到''

df1$Date[df1$TRIMMED.AVERAGE%in% c('', 'TRIMMED AVERAGE')] <- ''

或者可以使用data.table

完成此操作
library(data.table)#v1.9.5+
setDT(df1)[TRIMMED.AVERAGE %chin% c('', 'TRIMMED AVERAGE'), Date:='']