I am read the csv file, UMP into R and when I enter the print command, the data goes from a basic table that is not even anymore, and this is causing my plotted graph to become a complete mess.
How can I make sure my Excel CSV file is read properly?
Your table doesn't have an header.
Try:
data <- read.csv('mytable.csv',header=F)
colnames(data) <- c('year','value')
Related
hi literally day one new coder
On the excel sheet, my data looks organized, but when I upload my file to R, it's not able to read the excel properly and the column headers are in the rows and the data seems randomized.
So far I have tried:
library(readxl)
dataset <-read_excel("pathname")
View(dataset)
Also tried:
dataset <-read_excel("pathname", sheet=1, colNames=TRUE)
Also tried to use the package openxlsx
but nothing is giving me the correct, organized data set.
I tried formatting my Excel to a CSV file, and the CSV file looks exactly like the data that shows up on R (both are messed up).
How should I approach this problem?
I deal with importing .xlsx into R frequently. It can be challenging due to the flexibility of the excel platform. I generally use readxl::read_xlsx() to fetch data from .xlsx files. My suggestions:
First, specify exactly the data you want to import with the range argument.
A cell range to read from, as described in cell-specification. Includes typical Excel
ranges like "B3:D87", possibly including the sheet name like "Budget!B2:G14"
Second, if there are there merged cells or other formatting challenges in column headers, I resort to setting col_names = FALSE. And supplying clean names after import with names(df) <- c("first_col", "second_col")
Third, if there are merged cells elsewhere in the spreadsheet I generally I resort to "fixing" them in excel (not ideal but easier for my use case), however, others may have suggestions on a programmatic fix.
It may be helpful to provide a screenshot of your spreadsheet.
I have an Excel file where I have made a data connection (got my data from ODBC). I've also made a dataset in R which I now want to write into that existing Excel file. However, when I try to write the dataset into Excel, all of the data connections in there get lost - they're just tables, but not queries as they were before.
I have done that using openxlsx:
library(openxlsx)
wb <- loadWorkbook("myfile.xlsx")
writeData(wb,"Sheet1",mydataset,startCol=1,startRow=2)
saveWorkbook(wb,"myfile.xlsx",overwrite=T)
I have also tried excel.link:
library(excel.link)
xl.workbook.activate("myfile.xlsx")
xl.sheet.activate('Sheet1')
xl[a2] <- mydataset
and that almost works, it keeps the queries as needed but another problem appears - it ruins the encoding in that Sheet1. The special characters such as ä are now written as Ƥ.
Any ideas how I could fix the problem?
Thanks
Teele
I have an SPSS file from which I am creating a data table using R script. But the problem is it's taking a bit of time to load the data table. I want to convert the SPSS(.sav) file into CSV file first and then read the CSV file to create a data table. So far I have tried multiple codes but that didn't work out properly.
Here's the code which I got from this.
I think foreign package in r can be used to solve this problem.
library(foreign)
write.table(read.spss("inFile.sav"), file="outFile.csv", quote = TRUE, sep = ",")
I would like to clarify my understanding here on both converting a file into CSV and also reading it. Let's use a dataset from R for instance, titled longley.
To set up a data frame, I can just use the write.table command as follows, right?
d1<-longley
write.table(d1, file="", sep="1,16", row.names=TRUE, col.names=TRUE)
Has this already become a data frame or am I missing something here?
Now let's say if I want to read this CSV file. Then would my code be something like:
read.table(<dframe1>, header=FALSE, sep="", quote="\"")
It seems like before that I have to use a function called setwd(). I'm not really sure what it does or how it helps. Can someone help me here?
longley and, therefore, d1 are already data frames (type class(d1) in the console). A data frame is a fundamental data structure in R. Writing a data frame to a file saves the data in the data frame. In this case, you're trying to save the data in the data frame in CSV format, which you would do like this:
write.csv(d1, "myFileName.csv")
write.csv is a wrapper for write.table that takes care of the settings needed for saving in CSV format. You could also do:
write.table(d1, "myFileName.csv", sep=",")
sep="," tells R to write the file with values separated by a comma.
Then, to read the file into an R session you can do this:
df = read.csv("myFileName.csv", header=TRUE, stringsAsFactors=FALSE)
This creates a new object called df, which is the data frame created from the data in myFileName.csv. Once again, read.csv is a wrapper for read.table that takes care of the settings for reading a CSV file.
setwd is how you change the working directory--that is, the default directory where R writes to and reads from. But you can also keep the current working directory unchanged and just give write.csv or read.csv (or any other function that writes or reads R objects) the full path to wherever you want to read from or write to. For example:
write.csv(d1, "/path/for/saving/file/myFileName.csv")
I have a RMA normalized data ( from the CEL files ) and would like to write it into a file that I could open in excel but have some problems.
library(affy)
cel <- ReadAffy()
pre<-rma(cel)
write.table(pre, file="norm.txt", sep="\t")
write.table(pre, file="norma.txt")
The outut is arranged row-wise in the text file that is written using the above command and hence when exported to excel it is in a wrong form and many of the information is cut off as the maximum rows are used up .The output looks the following way :
GSM 133971.CEL 5.85302 3.54678 6.57648 9.45634
GSM 133972.CEL 4.65784 3.64578 3.54213 7.89566
GSM 133973.CEL 6.78543 3.54623 2.54345 7.89767
How to write it in a proper format from CEL files in R to a notepad or excel ?
You need to extract the values from the normalised probes using the exprs function. Something like:
write.csv(exprs(pre), file="output.csv", row.names=FALSE)
should do the trick.
I'm not totally clear about what the problem is, what do you mean by "proper format"? Excel will struggle with huge tables and doing your analysis in R with Bioconductor is likely a better way to go, you could then export a smaller results or summary table to excel.
Nevertheless, if you want the file written columnwise, you could try:
write.csv(t(pre),file="norm.txt")
But excel (at least used to) allow many more rows than columns.