Error in writing dataframe in csv - r

i have below dataframe
df_Place:
Name|Places
----+-----------------------
abc |delhi
bcd |mumbai,delhi
cde |chennai,hyderabad,delhi
def |mumbai
efg |bangalore,mumbai
ghi |delhi,bangalore
i wanted to have places in form of a matrix so i did below operation
df_Place$matrix<-as.matrix(strsplit(df_Place$Place,","))
i get below dataframe
Name|Places |matrix
----+-----------------------+------------------------------
abc |delhi |delhi
bcd |mumbai,delhi |c("mumbai","delhi")
cde |chennai,hyderabad,delhi|c("chennai","hyderabad","delhi")
def |mumbai |mumbai
efg |bangalore,mumbai |c("bangalore","mumbai")
ghi |delhi,bangalore |c("delhi","bangalore")
now while trying to write this into csv
write.csv(df_Place,"tx.csv")
i get below error:
Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol, :
unimplemented type 'list' in 'EncodeElement'
but if i remove the matrix column then it gets written successfully.
i know that it will be very basic, but can someone explain the reason behind this

It has to do with writing a matrix (with multiple dimensions) to a df in which multiple cols have no dimensions (vector). I found this solution to work (see Outputting a Dataframe in R to a .csv)
# First coerce the data.frame to all-character
df_Place2 = data.frame(lapply(df_Place, as.character), stringsAsFactors=FALSE)
# write file
write.csv(df_place2,"tx.csv")

You can use data.table library
fwrite(df_Place, file ="df_Place.csv")

Related

Error in match(x, table, nomatch = 0L) : 'match' requires vector arguments

I am trying to do some Bioconductor exercises on R studio cloud. Running the first two codes (#1,#2) have been fine, but the last code(#3) gives the error message. Please can anyone help?
#1 Transcribe dna_seq into an RNAString object and print it
dna_seq <- subseq(unlist(zikaVirus), end = 21)
dna_seq
21-letter "DNAString" instance
seq: AGTTGTTGATCTGTGTGAGTC
#2 Transcribe dna_seq into an RNAString object and print it
rna_seq <- RNAString(dna_seq)
rna_seq
21-letter "RNAString" instance
seq: AGUUGUUGAUCUGUGUGAGUC
#3 Translate rna_seq into an AAString object and print it
aa_seq <- translate(rna_seq)
aa_seq
aa_seq <- translate(rna_seq)
Error in match(x, table, nomatch = 0L) :
'match' requires vector arguments
aa_seq
Error: object 'aa_seq' not found
Thank you. I managed to solve the problem: I think there was a clash with the translate() function because it is used by both the seqinr and Biostring packages(I loaded both). I had to unload seqinr, because the exercises I was doing were based on the Biostring package.

warning in importing a CSV to R; "-" not meaningful for factors

I'm trying to import a set of dates from CSV to R.
Code:
dates <-- read.csv(file="dates.csv",header=FALSE)
Result:
Warning message:
In Ops.factor(left) : ‘-’ not meaningful for factors
The result is a bunch of NAs.
CSV file when opened in Notepad:
31/07/2014
30/07/2014
29/07/2014
28/07/2014
25/07/2014
24/07/2014
23/07/2014
22/07/2014
21/07/2014
18/07/2014
17/07/2014
16/07/2014
15/07/2014
14/07/2014
11/07/2014
10/07/2014
9/07/2014
8/07/2014
7/07/2014
4/07/2014
3/07/2014
2/07/2014
1/07/2014
30/06/2014
27/06/2014
26/06/2014
25/06/2014
24/06/2014
23/06/2014
20/06/2014
19/06/2014
18/06/2014
17/06/2014
16/06/2014
13/06/2014
12/06/2014
11/06/2014
10/06/2014
9/06/2014
6/06/2014
You can do this by using the <- operator, which is the standard assignment operator in R.
dates <- read.csv(file = "dates.csv", header = FALSE)
instead of the one you are typing above.
You could also read this for the different assignment operators used in R.

Writing and reading a zoo object - errors

I have a zoo object, prices, which, when I type class(prices), it returns “zoo.” I then create a file using:
write.zoo(prices, file = “foo”, index.name = “time”)
The resulting files looks like this:
"time" "AAPL.Adjusted" “SHY.Adjusted"
2013-05-01 60.31 84.12
2013-05-02 61.16 84.11
2013-05-03 61.77 84.08
I then try and read this file with this statement:
myData <- read.zoo(“foo”)
and I get this error:
Error in read.zoo(“foo") :
index has bad entries at data rows: 1 2 3 4
I’ve tried a number of parameter settings and nothing seems to work. Help much appreciated.
Newbie
The file has a header line so try:
z <- read.zoo("foo", header = TRUE, check.names = FALSE)
The check.names part gives nicer looking column names but you could leave it out if that were not important.

Undefined columns selected error in R while reading multiple files

I have lot's of files in my directoy and I want to read all files and select the second columns of them and put those columns as rows of a matrix, but I face with strange error.
would anybody help me to figure out, what's going wrong with my code ?
Here is my effort:
#read all files in one directoy into R and select desired column
nm <- list.files(path="April/mRNA_expression/")
Gene_exp<-do.call(rbind, lapply(nm, function(x) read.table(file=x,header=TRUE, sep= ",")[, 2]))
save(Gene_exp, file="Path to folder")
The error I get is :
## Error in `[.data.frame`(read.table(file = x, header = TRUE, sep = ""), :
## undefined columns selected*
To check that, really my files have 2 columns I did this :
b <- read.table("A.genes.normalized_results", sep="")
dim(b)
## [1] 20532 2
My text file Looks like this :
gene_id normalized_count
?|100130426 0.0000
?|100133144 10.6340
?|100134869 5.6790
?|10357 106.4628
?|10431 710.8902
?|136542 0.0000
?|155060 132.2883
?|26823 0.5098
?|280660 0.0000
?|317712 0.0000
?|340602 0.0000
?|388795 1.2745
?|390284 5.3527
?|391343 2.5489
?|391714 0.7647
?|404770 0.0000
?|441362 0.0000
The better solution would be to only import the second column when reading it. Use the colClasses argument to completely skip the first:
Gene_exp<-do.call(rbind, lapply(nm, function(x) read.delim(file=x,header=TRUE, colClasses=c('NULL', 'character'))))
I am assuming the second column is character. Change it to the appropriate class if you need to.

problems in deleting columns in csv file and storing data?

I have a csv file that has the following format:
1 3 1 4
1415670_at 1 8.512147859 8.196725061 8.174426394 8.62388149
1415671_at 2 9.119200527 9.190318548 9.149239039 9.211401637
1415672_at 3 10.03383593 9.575728316 10.06998673 9.735217522
1415673_at 4 5.925999419 5.692092375 5.689299161 7.807354922
I had made some manipulation of this data by deleting columns that are not 1 or 2:
m<-read.csv("table.csv")
smallerdat <- m[ c(1,2, grep("^X1$|^X2$|X1\\.|X2\\." , names(m) ) ) ]
Now I want to save this results again to a csv file, so I do this:
write.csv(smallerdat,"tablemodified.csv",ncolumns=length(smallerdat),sep=",")
but I got an error that says:
Error in cat(list(...), file, sep, fill, labels, append) :
argument 1 (type 'list') cannot be handled by 'cat'
The question that I have is how I can store into a csv file the modified table.
Any help?
The write.csv function needs to have the file-name given as a named argument (as do all of the write.* cousins). Try this instead (edited):
write.csv(smallerdat, file="tablemodified.csv" )
And my original guess applies to the save() function rather than the write.table variants.
I was about to tell you to read ?read.csv and note the "See Also" section that pointed to write.csv... but it doesn't.
So, use write.csv. :)

Resources