从R中的csv文件的列中替换几个字符

时间:2019-04-02 10:06:51

标签: r gsub

我有一个csv文件,如下所示:

> collar<-read.csv("41361_predicted_by_41365_cluster.csv",header=TRUE,stringsAsFactors = F)
> head(collar)
        observed      predicted probability
1 Moving/Feeding       Standing           1
2       Standing Feeding/Moving           1
3       Standing Feeding/Moving           1
4       Standing Feeding/Moving           1
5       Standing Feeding/Moving           1
6       Standing Feeding/Moving           1

我的问题很简单,但是由于我正在学习R,所以很难实现。我只需要在predicted列中进行以下字符替换:

1)将Feeding/Moving替换为Standing

2)将Moving/Feeding替换为Feeding/Moving

3)将Standing替换为Moving/Feeding

然后,我想使用名称"corrected_41361_predicted_by_41365_cluster"以csv格式写入数据。

我正在寻找最紧凑的方法。是否有与gsub()不同的方式?任何输入表示赞赏!

2 个答案:

答案 0 :(得分:2)

喜欢吗? (我对您的示例进行了一些更改,包括“移动/进纸”)

library(dplyr)

df %>%
  mutate(predicted = case_when(predicted == 'Feeding/Moving' ~ 'Standing',
                               predicted == 'Moving/Feeding' ~ 'Feeding/Moving',
                               predicted == 'Standing' ~ 'Moving/Feeding',
                               TRUE ~ predicted)) %>%
 write.csv(file = "corrected_41361_predicted_by_41365_cluster.csv")

文件包含结果:


        observed      predicted probability
1 Moving/Feeding Moving/Feeding           1
2       Standing       Standing           1
3       Standing       Standing           1
4       Standing       Standing           1
5       Standing Feeding/Moving           1
6       Standing       Standing           1

答案 1 :(得分:1)

您可以尝试这样的事情:

# Read all files in folder
mydocpath = "C:/Users/yourdata"
files <- list.files(path=sprintf("%s",mydocpath), pattern="*.csv", full.names=TRUE, recursive=FALSE)
print(files)

# Read to "list of DFs"
myf = lapply(files, function(x) data.frame(read.csv(x, sep=";", header=F, na = "na", skip=1)))
# Access single DF in list
myf[[1]]

# Do something to each DF
for(i in myf){
  print(head(myf))
}