繁体   English   中英

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM