I am writing a vector to a file in R. However the output comes on 2 lines. I wanted all the values to come on a single line. Can you let me know how to fix this
write(value,file=fileconn,append=TRUE,sep="\t")
The o/p comes as follows
1777.167 1825.167 1873.167 1921.167 1969.167
2017.167
Regards
Ganesh
I'm not sure write() is probably not the best choice, but if you want to use it, it might be a good idea to check the ?write help file. It does have an ncolumns= parameter which defaults to 5 for simple numeric vectors.
I would think cat() would be a better solution for just dumping numeric vectors.
Related
I just hope to learn how to make a simple statistical summary of the random numbers fra row 1 to 5 in R. (as shown in picture).
And then assign these rows to a single variable.
enter image description here
Hope you can help!
When you type something like 3 on a single line and ask R to "run" it, it doesn't store that anywhere -- it just evaluates it, meaning that it tries to make sense out of whatever you've typed (such as 3, or 2+1, or sqrt(9), all of which would return the same value) and then it more or less evaporates. You can think of your lines 1 through 5 as behaving like you've used a handheld scientific calculator; once you type something like 300 / 100 into such a calculator, it just shows you a 3, and then after you have executed another computation, that 3 is more or less permanently gone.
To do something with your data, you need to do one of two things: either store it into your environment somehow, or to "pipe" your data directly into a useful function.
In your question, you used this script:
1
3
2
7
6
summary()
I don't think it's possible to repair this strategy in the way that you're hoping -- and if it is possible, it's not quite the "right" approach. By typing the numbers on individual lines, you've structured them so that they'll evaluate individually and then evaporate. In order to run the summary() function on those numbers, you will need to bind them together inside a single vector somehow, then feed that vector into summary(). The "store it" approach would be
my_vector <- c(1, 3, 7, 2, 6)
summary(my_vector)
The importance isn't actually the parentheses; it's the function c(), which stands for concatenate, and instructs R to treat those 5 numbers as a collective object (i.e. a vector). We then pass that single object into my_vector().
To use the "piping" approach and avoid having to store something in the environment, you can do this instead (requires R 4.1.0+):
c(1, 3, 7, 2, 6) |> summary()
Note again that the use of c() is required, because we need to bind the five numbers together first. If you have an older version of R, you can get a slightly different pipe operator from the magrittr library instead that will work the same way. The point is that this "binding" part of the process is an essential part that can't be skipped.
Now, the crux of your question: presumably, your data doesn't really look like the example you used. Most likely, it's in some separate .csv file or something like that; if not, hopefully it is easy to get it into that format. Assuming this is true, this means that R will actually be able to do the heavy lifting for you in terms of formatting your data.
As a very simple example, let's say I have a plain text file, my_example.txt, whose contents are
1
3
7
2
6
In this case, I can ask R to parse this file for me. Assuming you're using RStudio, the simplest way to do this is to use the File -> Import Dataset part of the GUI. There are various options dealing with things such as headers, separators, and so forth, but I can't say much meaningful about what you'd need to do there without seeing your actual dataset.
When I import that file, I notice that it does two things in my R console:
my_example <- read.table(...)
View(my_example)
The first line stores an object (called a "data frame" in this case) in my environment; the second shows a nice view of how it's rendered. To get the summary I wanted, I just need to extract the vector of numbers I want, which I see from the view is called V1, which I can do with summary(my_example$V1).
This example is probably not helpful for your actual data set, because there are so many variations on the theme here, but the theme itself is important: point R at a file, as it to render an object, then work with that object. That's the approach I'd recommend instead of typing data as lines within an R script, as it's much faster and less error-prone.
Hopefully this will get you pointed in the right direction in terms of getting your data into R and working with it.
I have a quick question. I am using tables::tabular() and have some summary statistics which are displayed in scientific e notation.
I found out that using Format(scientific=FALSE) * mean() in this context helps me to get rid of the scientific e notation in the means, and other summary statistics i present.
Now, I also want to round this number with the format 0.123456789 (most of the means are means of ratios between 0 and 1 but not all) to show the digits left and maximum 4 to the right of the comma i.e. 0.1234. I tried simply putting the digits=4 option into the Format and while it seems to work when alone in the Format() function, it doesn't somehow when I also have scientific=FALSE in there. Rather, with Format(scientific=FALSE, digit=1), I still get 5 digits to the right of the comma.
Do you know what is happening and how i can fix it?
Highly appreciate your help,
cork
Have you already tried to use a global statement at the beginning like this: options("digits" = 4)?
All the best,
Patrick
I am confused about the paste() function in R.
This is my r code:
paste(cols=list("speed###","dist"), rows=list("speed"))
The ideal output should be:
cols=list("speed###","dist"), rows=list("speed")
But the actual output was:
"speed### speed" "dist speed"
Can anyone help me to figure out and get the ideal output?
I will appreciate any reply here!
Thank you!
Best regards!
What I think is happening with your code is:
You are passing list("speed###","dist") and list("speed") as parameters cols and rows, not as strings, and paste ignores names of parameters. Also, lists are converted to characters:c("speed###","dist"), c("speed")
Since parameters don't have the same length, the second is replicated (i.e. c("speed###","dist") and c("speed", "speed")).
Then, first elements are pasted together, and second elements are as well, returning a vector with each pasted string: c("speed### speed", "dist speed")
Why do you need what you are asking for? I mean, you plan to do.call(matrix, list(cols=c("speed###","dist"), rows=c("speed"))) or something?
Besides, you should use c instead of list. I don't understand the need of using list here.
Basically, I want to write a program to transform R code into Latex formulas. For example, I want to build some sort of converter where, when I have, as input, mean(x) it will return $\frac{1}{n}\sum_{i=1}^{n} x_{i}$ - latex code for the formula. I would to this for a group of formulas I have to basically make my job easier.
Although this might be some work, I would still like to do it. Basically I would tell the program to return $\frac{1}{n}\sum_{i=1}^{n} everytime he finds the word mean; to, by default, transform x into x_{i}, this type of thing.
What language should I use to build such a program? Or does this already exist? I've looked online and found nothing of the sort... Would you say this is extremely difficult to do?
Thanks everyone!
I am fairly new to R. I have a datafile which has a matrix of complex numbers, each of the form 123+123i, when I try to read in the data in R, using read.table(), it returns strings, which is not what I want. Is there some way to read in a file of complex numbers?
One possible thing that I could do, since the program that generates the matrix is available to me, I can modify it to generate two real numbers instead of a single complex number, and after reading into R, I can make them into a single complex number, now would this be the canonical way to doing what I want?
See ?read.table, in particular you want to use the colClasses="complex" argument.