R - 在 r 中用逗号检查列索引

R - check column index with commas in r

我想检查从外部加载的数据框,如果我发现 commas 而不是 dots,我想替换它(简单)但是还要说一下我在哪一栏找到了逗号(那是我不知道该怎么做)

所以我从类似的东西开始(当然我要检查的列不止两列)

dat <- read.table("data.csv", header=TRUE, sep=";", dec=".")

#    d1  d2
# 1 0.1 0,2
# 2 0.2 0,4
# 3 0.4 0,3
# 4 0.6 0,1
# 5 0.7 0,5

for (col in colnames(dat)) {
  if (dat[,col].....) { # here the missing control
    dat[,col] <- as.numeric(gsub(",", ".", data[,col]))
    warnings(sprintf("Replaced commas by dots in column %s", col))
  }
}

我们可以使用 lapply 遍历列并检查其中是否包含 ",".

lapply(dat, function(x) any(grepl(",", x)))
#    $d1
#[1] FALSE
#
#$d2
#[1] TRUE