Filter and save to csv

Asked

Viewed 1,219 times

4

I would like to perform a filter on R. I have a database with 5 variables and more than 5000 lines. One of my variables is "room", in which I have 29 room (ex:Sala1,sala2...).

I would like to perform a filter, to filter all the information that it contains in my database for a particular room, and then export this information in a csv file.

I even made a program, but it is not storing right in a csv file. I would like someone to give me a tip for better my code and/or help me solve this storage problem. Below follows my code.

dados=read.csv("C:/teste/dados.csv", sep=";",header=TRUE) 
nome=dados$nome
perfil=dados$perfil
sala=dados$sala
forum=dados$forum
mensagem=dados$mensagem
BD=data.frame(nome,perfil,sala,forum,mensagem)
x2=subset(nome,sala=="(T2/14/EI) D01 - Diversidade e Cultura Inclusiva")
x3=subset(perfil,sala=="(T2/14/EI) D01 - Diversidade e Cultura Inclusiva")
x6=subset(sala,sala=="(T2/14/EI) D01 - Diversidade e Cultura Inclusiva")
x7=subset(forum,sala=="(T2/14/EI) D01 - Diversidade e Cultura Inclusiva")
x10=subset(mensagem,sala=="(T2/14/EI) D01 - Diversidade e Cultura Inclusiva")
dados1=paste(x2,x3,x6,x7,x10,sep=";")
cat("nome;perfil;sala;forum;mensagem", dados1, file="arquivo.csv", sep="\n",header=T)
  • What is the current code behavior?

  • So Jjoao he is even performing the filtering properly, but when I ask to store in a csv file, he is storing everything "messy".

  • What code are you using to generate . csv? Is your data.frame when you write . csv correct? I recommend using the command: write.csv(seuDataFrame, file = "seusDados.csv") to generate . csv from a dataframe

  • I’m using this: data1=Paste(x2,X3,X6,X7,X10,Sep=";") cat("name;profile;room;forum;message", data1, file="file.csv", Sep=" n",header=T). I didn’t know that command, I’ll try to use

  • It is worth taking a look at this question also on ways to filter a data.frame: http://answall.com/questions/17439/como-filterr-um-data-frame/17479#17479

1 answer

4


To save in the formed CSV to use the write.csv2() (and to read, read.csv2()). Besides, you can use the subset() in a data.frame, greatly simplify your code:

dados <- read.csv2("C:/teste/dados.csv", header=TRUE)
write.csv2(subset(dados, sala == "(T2/14/EI) D01 - Diversidade e Cultura Inclusiva"), "arquivo.csv",  row.names = FALSE)

The parameter row.names = FALSE serves to take the column with the number of the row (I personally think it only disturbs)

  • Thank you very much, thank you very much! You have helped my life so much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.