It's my first post here. I'm a beginner in R maybe my problem is not very complicated.
I used mygene package from R to create an annotation for our RNAseq results. It looks nice and it generated a res list wit 3 sublists as follows (summary):
Length Class Mode
response 8 DataFrame S4
duplicates 255 data.frame list
missing 3584 -none- character
I was interested in the first list response
When I'm calling it in R looks ok:
but after exporting that as a txt file with that code:
write.table(res$response, "res.response_test.txt",quote=F,col.names = T,sep = "\t")
In the txt files some lines looks like creaked in wrong places:
I would be grateful for your help with exporting that result.
Related
I am trying to test unicode-heavy imports of various R packages. I'm through everything but JSON because of a persisetant error: The file is read in as one long, single-row file. The file is available here.
I think I am following the instructions in the help. I have tried two approaches:
read the data into an object, then convert to a data frame.
raw_json_data <- read_file("World Class.json")
test_json <- fromJSON(raw_json_data)
as.data.frame(test_json)
Read the file using fromJSON() then convert to a data frame. I happen to be using R's new pipe here, but that doesn't seem to matter.
rjson_json <- fromJSON(file = "World Class.json") |>
as.data.frame()
In every attempt, I get the same result: a data frame of 1 column and 1400 variables. Is there a step I am missing in this conversion?
EDIT: I am not looking for the answer "Use package X instead". The rjson package seems to read in the JSON data, which has a quite simple structure. The problem is that the as.data.frame() call results in one-row, 1400-character data frame, and I'm asking wht that is.
Try the jsonlite package instead.
library(jsonlite)
## next line gives warning: JSON string contains (illegal) UTF8 byte-order-mark!
json_data <- fromJSON("World Class.json") # from file
dim(json_data)
[1] 40 35
I am attempting to get a cartesian combination of 3 data frames in RStudio and then have that output sinked to a file. I used the crossing function to get the combinations and the sink function to have the output added into a file. The code works and provides the Cartesian product of the 3 data sets (verified in the environment tab), but the output only shows the first 10 lines of output. I have tried options(max.print=999999), to no avail. This is what it says after the first 10 lines: # … with 61,992,438 more rows. I am trying to have it show all rows so that all rows are coded to the file. I've looked around stack and haven't found any answers to this. My code (yes its very messy as of now) is attached below:
library(tidyr) # Loads in the crossing and expand functions
Q14fb=read.csv("q14fb.csv") # Reads in the data sets
Q13e=read.csv("Q13e.csv")
Q14e=read.csv("Q14e.csv")
sink(file = "Q3combooutput.txt", append = TRUE, split=TRUE) # Creates a file
x=data.frame(x=c(Q13e)) # Codes in the data
y=data.frame(y=c(Q14e))
z=data.frame(z=c(Q14fb))
result=crossing (x,y,z) # Provides all possible combinations of the data sets
result # Provides the output
sink(append=TRUE) # Adds the output to the file
The problem
sink() will only print output from the R Console to the file. Since result was created by tidyr::crossing(), it is a tibble, and the default printing for tibbles are just the first 10 rows.
Some solutions
You can print all lines of a tibble by wrapping it in print(result, n = Inf). However, you are probably better of using some of the standard ways of exporting a file from R, such as write.csv() to export a CSV file.
I am trying to read data out of a csv-file.
The data consists of small integer numbers (53, 98 ...)
The csv was made with OpenOffice, the data stood there in the first column
one number in each row.
reading data was simple (no problem at all):
BirthNumbers <- read.csv(“/Users/.../RawData.csv”, header=FALSE)
Now I try to calculate mean(BirthNumbers) (for example),
but it is not possible, the error message:
x is not numeric
Where is my mistake?
Thanks for all help
Norbert
It's probably being read in as characters.
Try mean(as.numeric(BirthNumbers))
As per https://stat.ethz.ch/R-manual/R-devel/library/utils/html/read.table.html (see Value section), read.csv returns a data frame.
You should be calling mean on the column of the data frame. Since you have no headers (given your header = FALSE), most likely the column is called V1 (verify by doing head(BirthNumbers) or colnames(BirthNumbers)), so you should do mean(BirthNumbers$V1).
I´m trying to do redundancy analysis (RDA) on my data in R. The data frame I´m using was uploaded as a Microsoft Excel csv file. The data frame looks something like this:
site biomass index
1 0.001 1.5
2 0.122 2.3
3 0.255 4.9
When trying to create a formula for the RDA, I constantly get the following message: "Error in formula.data.frame(object, env = baseenv()) :
cannot create a formula from a zero-column data frame"
Does anyone know how I can change my data frame so that I no longer get this error message?
Thanks in advance!
You load all your data to row.names instead of columns
you used default separator ',' while your data is separated by
';'
you specified row.names = 1 so that first column (and the
only one as the separator is wrong) goes to row.names.
that's why your data.frame has no columns. To fix this, use
read.csv('data.csv', sep = ';', row.names=NULL)
Problem solved :) Thanks for the your answers. I tried the suggestions but what ended up working was to import the data with the <- read.csv2("data.csv", row.names=1), so basically to use read.csv2 instead of read.csv, as it seems to be used in many European locales.
There is more info on the difference between csv and csv2 here: Difference between read.csv() and read.csv2() in R
I have imported a CSV file to R but now I would like to extract a variable into a vector and analyse it separately. Could you please tell me how I could do that?
I know that the summary() function gives a rough idea but I would like to learn more.
I apologise if this is a trivial question but I have watched a number of tutorial videos and have not seen that anywhere.
Read data into data frame using read.csv. Get names of data frame. They should be the names of the CSV columns unless you've done something wrong. Use dollar-notation to get vectors by name. Try reading some tutorials instead of watching videos, then you can try stuff out.
d = read.csv("foo.csv")
names(d)
v = d$whatever # for example
hist(v) # for example
This is totally trivial stuff.
I assume you have use the read.csv() or the read.table() function to import your data in R. (You can have help directly in R with ? e.g. ?read.csv
So normally, you have a data.frame. And if you check the documentation the data.frame is described as a "[...]tightly coupled collections of variables which share many of the properties of matrices and of lists[...]"
So basically you can already handle your data as vector.
A quick research on SO gave back this two posts among others:
Converting a dataframe to a vector (by rows) and
Extract Column from data.frame as a Vector
And I am sure they are more relevant ones. Try some good tutorials on R (videos are not so formative in this case).
There is a ton of good ones on the Internet, e.g:
* http://www.introductoryr.co.uk/R_Resources_for_Beginners.html (which lists some)
or
* http://tryr.codeschool.com/
Anyways, one way to deal with your csv would be:
#import the data to R as a data.frame
mydata = read.csv(file="SomeFile.csv", header = TRUE, sep = ",",
quote = "\"",dec = ".", fill = TRUE, comment.char = "")
#extract a column to a vector
firstColumn = mydata$col1 # extract the column named "col1" of mydata to a vector
#This previous line is equivalent to:
firstColumn = mydata[,"col1"]
#extract a row to a vector
firstline = mydata[1,] #extract the first row of mydata to a vector
Edit: In some cases[1], you might need to coerce the data in a vector by applying functions such as as.numeric or as.character:
firstline=as.numeric(mydata[1,])#extract the first row of mydata to a vector
#Note: the entire row *has to be* numeric or compatible with that class
[1] e.g. it happened to me when I wanted to extract a row of a data.frame inside a nested function