Richard, your definition is, of course, not valid R code.
I assume the pseudocode is easier to write in a language like python and says
that you want to loop through all items and compare each item to every other
item in a second similar loop (meaning it happens twice and also compares
items t
Daniel,
Do an experiment and see what happens, works lots of time.
I note your question may be what happens if you use factors, as in
categorical data. Factors have two sides with one being a numerical index of
sorts and the other one being a character string.
If I make two vectors for illustr
This is a bit silly. Anyone who wants to type less has many trivial ways to do
so by themselves. And, why call it "span" when something like "a1" is even
shorter! LOL!
You can choose to shorten even many built-ins for many purposes and perhaps
make your programs a tad of a challenge to read.
I did not intend to segue into the fates of other languages except insofar as R
is like many others that can be evolving while both attracting new "clients"
and uses and shedding others including g some who decide some other language
may suit them best.
I am happy to hear versions of PASCAL are
John,
You made me wonder if pipes add anything new to R. I happen to think they do
and one argument to consider is that starting with the Magrittr pipe and on
into the tidyverse, many people choose to use them quite a bit. Note that the
ggplot2 graphics in various packages use something similar
Richard,
You remind me of my excursions over the years into many languages, including
some that may be extinct. Is PASCAL used anywhere, for example? I wrote lots of
code in it once. Others that I know are around have morphed as in variations
of LISP. I was amused to hear from a friend that h
I would add to what Jeff replied. Many and perhaps most or even all
languages that have room for evolution, including Python, can end up getting
more and more complex with multiple ways to do things but it generally is
possible to write many useful programs in the core language.
I often wonder wha
Mike,
There is in a mathematical sense, no difference between:
1 - x
and
-x + 1
But for code that is compiled or interpreted, I suspect various strategies may
make one be acted on a bit differently. Does the hardware for addition work
differently than for subtraction? Does it implement -x by
Rolf,
If it is homework, I have to wonder at what level the class is before any of
the proposed answers would be considered acceptable by the instructor. For a
true beginner who has not even learned that much of R is vectorized, we are
back to perhaps using a manual loop with indices and ...
I
Bert,
I noted similar things which indicate the limits a statistician or a programmer
in R can run into about what can be measured as meaningful.
But, you can get numbers out of a system that can be helpful even if some
confounding factors are not accounted for.
I also have sleep apnea and h
Actually, what I would love to be discussed here as more On Topic is which
functions and packages commonly used in R use the various kind of methods you
mention. Are newer packages focused some way or another?
What are the drawbacks and advantages. As an example, if a simulation method
can retu
Kevin,
What some call Nerdy is actually a meaningful and interesting activity to
others. Your examples are quite a bit like all kinds of things in our lives
that we suddenly decide to measure. I, for example, keep track of books I
read and use R to calculate interesting (to me) statistics about ho
A brief answer to this OT question is that many disciplines do the same
thing and teach multiple methods, including some that are historical and are
no longer really used.
But since you say this was an intro course, it would not prepare you well if
later courses and the real world expose you to us
Jeff,
There are multiple agendas here. Yes, lots of items we have do use power
even when not in use or even when supposedly turned off. Devices like a TV
that is instant-on because some circuitry is on and listening for the remote
control and ready to just turn the screen power on are another exam
This too is a question, not really an attempt to provide an answer.
What has happened with the proposed S7 object that can loosely be seen as an
effort to somewhat merge S3 and S4 and perhaps more?
Back to what Erin wrote that is interesting.
If a package creates stand-alone objects that are pr
Arnaud,
I won't comment on other aspects but want to ask how sure you are that your
data is guaranteed to have a single row reflecting a closing price at 18:59:59
exactly?
It may be true for your data source. I note that markets technically close at
4:00 PM, New York time, but many have after-
Thanks for the clarification, Richard, as I clearly made the wrong guess of
what you meant.
Your idea or objection was that you see the included read.csv function as
adequate and see no incentive to use read_csv, and especially not if that is
the only function being used. I only partially agree
Richard,
After some thinking, I conclude that founders of things, including ancient
religious figures as well as people who have developed programming languages,
may not recognize what follows later. As an example, you have customs in some
religions revolving around trees and snow or around pot
Richard,
The function with a period as a separator that you cite, read.csv, is part of
normal base R.
We have been discussing a different function named just a tad different that
uses an underscore as a separator, read_csv that is similar but has some
changes in how it works and the options su
Rui,
I just want to discuss this point you made for now:
2.
Systematic use of apply(., 1, mean).
rowMeans (and colMeans) are much faster.
You are, of course, technically correct. I consider an alternative aspect in
which you are teaching a programming language as a small
Kevin & Richard, and of course everyone,
As the main topic here is not the tidyverse, I will mention the perils of
loading in more than needed in general.
If you want to use one or a very few functions, it can be more efficient and
safe to load exactly what is needed. In the case of wanting to
Kevin,
I was amused by the use of the parentheses wrapping to get the REPL to show the
effects of an assignment but would remove that in any final program if the
output is not needed.
I am not saying this is wrong, nor what I describe below, but just a discussion
of how others might do it.
I
Kevin,
It is simple. Your matrix has fifty entries and you supplied just 10. R
tends to quietly assume you want the sample repeated as often as needed as
long as it can be used in whole amounts. So, you get five copies. If you
interchanged rows and columns with byrow=FALSE then every two rows woul
I like your examples, Richard, of strings that should not be considered as
numbers. There are times you want to recognize something like "221B Baker
Street" and extract the 221 or perhaps the 221B as a part of a full address and
ignore the rest. But one goal of a function like as.numeric() is to
The second solution Ivan offers looks good, and a bit more general than his
first that simply removes one non-visible character.
It begs the question of why the data has that anomaly at all. Did the data come
from a text-processing environment where it was going to wrap there and was
protected?
Rui,
Your post (way at the bottom) expands beyond what was requested and that makes
it more interesting.
I like to look at what I consider reusable ideas and tools.
One was the reminder of using apply across rows of a matrix which would have
applied to my earlier post of a package that also c
Dennis,
You are supposedly using a vector containing integers, in order, and want to
find sequences to rewrite compactly based on your example.
What result do you want. An integer vector cannot hold something like 3-5 so
do you want an integer of character stringls like this:
c("1", "3-5", "7-8"
That is an interesting fix Duncan suggested and it sounds now like everything
WORKED as intended in data.table except that any checker being used externally
is not able to del with things that look like a variable but are actually not a
variable currently visible except within a function that is
Naresh,
I think your details suggest your earlier request was not well defined.
Your problem looks like this line of code was wrong and did something you did
not expect. It should be easy to fix. But I note you changed Prices to use the
data.table package. Mind you, I thought the function
Naresh,
I am not sure how you are creating your data.frame so it has no, I think,
column names. There are two scenarios including one where it is not really a
valid data.frame and one where it can be handled before any other use as shown
below. If it cannot be used, you might need to modify how
John,
Others have helped educate you on your initial guesses and I just want to
add that "str()" is not the only function people use to see their data.
People often want to also look at what class(es) an object has or the names
inside it and so on or what dimensions it contains. There are various
John,
What would be nice is not necessarily trivial.
Consider an example. After running a line of code with an assignment statement,
did you know the last value has been saved in .Last.value as in this snippet of
code:
> x <- 12
> .Last.value
[1] 12
> y <- .Last.value**2
> y
[1] 144
> .Last.va
Arguably, R was not designed or evolved for truly novice users, nor really was
Python or just about all computer languages. As they evolved and became in many
ways more powerful, they tended to get ever less user friendly in the way you
are asking for and gotten so bloated that many features are
Like many things in R, the original way things were done may have ossified
in place and even if largely unknown packages came along, may not be known
by many.
The topic John is talking about is NOT in my mind about systems programming
at all. It is about writing any function where you want control
Long time readers indeed.
That article was from 2009!
-Original Message-
From: R-help On Behalf Of Enrico Schumann
Sent: Sunday, December 15, 2024 5:27 AM
To: Bill Dunlap
Cc: r-help@r-project.org
Subject: Re: [R] [off-topic] crossword
On Fri, 13 Dec 2024, Bill Dunlap writes:
> Cross
It is probably a bit late in most existing languages to change behaviors all
over the code. Languages like C often allowed many kinds of shortcuts that
happened to work because 0 was false and 1 and most other things were true.
Similarly, pointers were used in a Boolean way as in the way they co
Ivo,
To be a bit clearer, your problem was not with mean(0 but with any function
which is expecting a Boolean setting of TRUE/FALSE and instead got a
nonsense value of 20.
This gets even weirder in languages which automagcally transform many other
values to be considered true. Languages like C co
Not to beat a dead horse, but if they wanted an answer of CODES, then why not
use S instead of R in the …
From: Bill Dunlap
Sent: Friday, December 13, 2024 3:47 PM
To: avi.e.gr...@gmail.com
Cc: Olivier Crouzet ; r-help@r-project.org
Subject: Re: [R] [off-topic] crossword
Crossword answer
I hesitate to say this and stray back on topic.
R is basically an extension of C as it was written in C and many added
functions end up being rewritten in a variant of C, albeit other languages may
at times intrude. Something similar could be said of C-Python.
But I truly doubt the ones making
Since this is a discussion about a specific crossword puzzle the right answer
has to fit with any words coming in from the other direction or it gets cross.
I thought the clue hinted it started with C and ended with R and that the
languages were chosen for no reason other than that they helped m
Actually, tangentially, JC, I have a deep suspicion that many computer
languages are not written to solve problems. They are sometimes an effort by
someone to implement a new paradigm different than what others have tried
before or to protect the programmer from themselves or to demand extensive
ri
Akshay,
Your question has way too many answers.
SQL has a long history and early versions came long before R arrived on the
scene. There is a huge embedded base of hardware and software dedicated to
managing databases. It has some features that most R programs do not even
dream of doing. Besides
What exactly is stopping you, Carlos?
Do you know where the reagent dataset is? I don't. Is it being loaded from a
known format like a .CSV file, or part of an EXCEL spreadsheet and so on?
Does it come with a function in a package that you load and then invoke to
load it or make it visible? Or, s
John,
I hate to break it to you that ggplot2 is not part of base R. "plot" and
maybe lattice qualify.
But I can work with you. Try this:
ggplot(data=mydata,
aes(x=AQIGroup,
y=Freq,
group=Day,
color=Day)) +
geom_point() +
geom_line()
The main change b
John,
Thanks for enlightening us so we better understand.
I won't argue with your wish to learn to do things in base R first. I started
that way, myself, and found lots of the commands not particularly easy to fit
into a single worldview. Many functions I read about were promptly forgotten,
es
I was wondering along similar lines, Bert.
One way to get help is to ask how to do some single step of a larger strategy.
That can lead to answers that may not be as applicable to the scenario.
Another way would be to include a synopsis of what they are trying to do.
But, as John says he is try
John,
If I understood you, you want to take the minimum value of Day for each
grouping by ID and add a new column to contain that. Right?
There are likely many ways to do this in base R, but I prefer the
dplyr/tidyverse package in which you can use group_by(ID) piped to
mutate(FirstDay = min(Day
I am curious what you mean by "having a graph."
If you mean something drawn on paper or an image on your screen, then
extracting specific data at various points is an effort, perhaps more so if
a wind rose plot does not contain all you need. If you have something like a
ggplot2 object, it tends to
I have had experiences with people too daunted by a programming language
where all kinds of small mistakes just generate an error. They often just
want to do a few things and do not want to learn programming in depth.
They want the kind of GUI interface that was often made for people coming
from s
Jacob,
I am curious about your request for a "minimum" and it makes me wonder what
aspects of switching to Windows 11 from earlier versions (no longer being
supported around now) have some impact on R.
Many people want to be running as recent a version of R as possible, albeit
there can be proble
Grant, I think,
Your NO SUBJECT message confused me as it seems a continuation of an earlier
discussion of a new and likely irrelevant metric of the worthiness of R
programs.
Did you make a mistake here? I tried your code as well and the results did
not look like what the OP asked for.
It took m
Admit it, Rolf. Haven't you wondered if S, in a more private way, is sexier
than R?
OK, kidding aside, we have talked this to death.
Just FYI, the conversation was stimulating for some of us and I have continued
on my own and located functions I see as useful in the stringi and stringr
package
John,
I thought some more about the topic overnight. Of course, "sexy" is not a
great analogy.
But consider a concept of how to do something cleverly or creatively or in
ways others might not easily come up with as the standard way(s) are
commonly used.
I threw something together in middle of th
Calum,
I know Rolf for a while so I will not accept any calumny about his intentions.
He stated what he wanted, albeit imperfectly, and interacted with us as we came
up with ideas.
I have seen others who ask some open-ended question, often using a brand new
idea, and do not interact. Som
I see a book coming:
"666 ways to do the same thing in R ranked by sexiness."
Kidding aside, if you look under the covers of some of the functions we are
using, we may find we are taking steps back as some of them use others and
perhaps more functionality than we need.
But for a new rea
You are, of course, correct, John. But in a strange way, many people end up
finding their wife or husband not so sexy after many years and find others
now seem to be.
R is not about sex and it was not the ideal choice of words.
I think what was wanted was something brief rather than taking many l
Rold,
We need to be clear on what makes an answer sexy! LOL!
I decided it was sexy to do it in a way that nobody (normal) would and had
not suggested yet.
Here is an original version I will explain in a minute. Or, maybe best a bit
before. Hee is the unformatted result whicvh is a tad hard to re
Rolf,
I, and many others have come up with an assortment of solutions that seem to
work, often by ignoring whatever you intend by mentioning f as a factor.
But consider a dumb question. Why are you starting with a list of vectors,
with odd pseudo-numeric names?
Many of the solutions started by c
Rolf,
This works, albeit you may not be thrilled:
x <- list(`1` = c(7, 13, 1, 4, 10),
`2` = c(2, 5, 14, 8, 11),
`3` = c(6, 9, 15, 12, 3))
as.vector(rbind(x[[1]], x[[2]], x[[3]]))
-- output:
> as.vector(rbind(x[[1]], x[[2]], x[[3]]))
[1] 7 2 6 13 5 9 1 14 15 4 8 1
Stefano,
I see you already have an answer that works for you.
Sometimes you want to step back and see if some modification makes a problem
easier to solve.
I often simply switch to using tools in the tidyverse such as dplyr for parts
of the job albeit much of the same can be done using functio
Bert,
Although the Яticle was interesting, I have to wonder how much publishing there
has been in formal journals related to R, especially recently, that is of a
research variety.
I am thinking of an example and wonder if we picked something that is often
re-implemented by many parties such as
Excellent message, Gabor.
Many tools we use are quite flexible and I just want to mention dplyr does have
ways to use something like mutate to rename a column, albeit rename(0 is more
specifically designed to do the job.
Here is an example of how mutate() can rename by making a new column and
As an intellectual exercise it can be reasonable to discuss these ways to use a
pipe even in places where it may not have been seen as something anyone would
even try to use it.
In actual code, it is often better to not make overly cute constructions that
others (or yourself a month later) will
Bert,
You need to consider LHS vs RHS functionality.
Before I start, I would have done your example setup lie this:
trio <- 1:3
z <- data.frame(a = trio, b = letters[trio])
Just kidding!
Syntactic sugar means you are calling this function:
> `names<-`
function (x, value) .Primitive("names<-"
Just to be clear, Denis, I am not in any way associated with anybody or
anything and just read about it on a news feed not from POSIT directly.
I am aware it is based on existing functionality and have used possibly similar
editors for other languages. I did try possibly one touted by Microsoft
This is just an FYI based on a news item I saw tonight.
There have been discussions on what editors or environments people can use
when working with R and I personally have mostly been using versions of
RSTUDIO and lately for both R and python. As often noted, RSTUDIO is a
product of a company, c
Unfortunately, Adrian, even trying to coerce a string like "--" to numeric just
produces an NA and comparison fails:
> as.numeric("--") < 0
[1] NA
Warning message:
NAs introduced by coercion
The same is true of anything that is character as it cannot be coerced.
> as.numeric("anything") < 0
[
I had the same FIRST impression which could have made sense as in writing
the data back out to a file with a similar name and that would have been
trivial. I mean if you had exactly three, a loop would not even be
particularly useful versus writing two lines, copying them and editing the
latter two
I fully agree with Jeff that the best way to use ANY language is to evaluate
the language in terms of not just the capabilities it offers but also the
philosophy behind what it was created for and how people do things and just
grok it and use it mostly in the way intended. I do that with all the
la
Laurent,
Thank you for introducing me to a package I did not know existed as I use
features like list comprehension in python all the time and could see using it
in R now that I know it is available.
As to why you see your example as slow, I see you used a fairly complex and
nested expression
Bert,
I think you read my message differently than I expected. I approached the
request as an exercise in evaluating various ways something can be done and
maybe choosing a simple one or choosing a more general one as needed.
I provided some solutions along the lines you mentioned and THEN also
For the particular example you asked for, consider the "each" you can use
with rep()
rep(1:13, each=84)
This is what it does for a shorter version of 4 each:
> rep(1:13, each=4)
[1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6
7 7 7 7 8 8 8 8 9 9 9 9 10 10
John,
Maybe you can clarify what you want the output to look like. It took me a
while to realize what you may want as it is NOT properly described as
wanting rowsums.
There is a standard function called rowMeans() that probably does what you
want if you want the mean of all rows as in:
> rowMean
Although there may well be many ways to do what is being asked for with the
tidyverse, sometimes things are simple enough to do the old-fashioned way.
The request seems to have been to do something to all rows in ONE specific
group but was phrased in the sense of wanting to know which group your
Nick,
As Jeff said, we don't know what you tried and what did not work.
There are built-in and probably package versions but have you tried
something like list.files()?
You can tweak it to get the files you want by doing something like:
-change directory to HERE
- here.files <- list.files(recur
I think it might be fair to say that the discussion is becoming a tad wider
than whether you want your data structures indexed starting from 0 or 1.
Programming languages have added functionality to do many things on top of
the simple concept of accessing or changing the nth element one at a time.
Hans,
It is a good question albeit R made a conscious decision to have indices
that correspond to things like row numbers and thus start with 1. Some
others have used a start of zero but often for reasons more related to
making use of all combinations of the implementation of integers on many
mach
Yes, Lennart, I have been looking at doing something like you say by using the
vectorized ways the tidyverse is now offering.
For my application, if the naming was consistent, an approach like yours is
good, albeit has to be typed carefully. When I cannot control the names but
have to lump th
Richard,
The code you show is correct and it does not include where you say ChatGTP
explained it was 33/26 rather than the correct 42/216.
I gather it have the proper fraction for the other two scenarios.
So what would cause such a localized error?
The method chosen is to try all possible combi
Thanks everyone and any/all reading this. I think I got my answer. And, no, I
suspected I did not need to provide a very specific example, at least not yet.
The answer is that my experiment was not vectorized while using dplyr verbs
like mutate do their work implicitly in a vectorized way.
Thi
Base R has generic functions called any() and all() that I am having trouble
using.
It works fine when I play with it in a base R context as in:
> all(any(TRUE, TRUE), any(TRUE, FALSE))
[1] TRUE
> all(any(TRUE, TRUE), any(FALSE, FALSE))
[1] FALSE
But in a tidyverse/dplyr environment, it retur
Dave,
Your method works for you and seems to be a one-time fix of a corrupted data
file so please accept what I write not as a criticism but explaining my
alternate reasoning which I suspect may work faster in some situations.
Here is my understanding of what you are doing:
You have a file in
It sounds like the discussion is now on how to clean your data, with a twist.
You want to clean it before you can properly read it in using standard methods.
Some of those standard methods already do quite a bit as they parse the data
such as looking ahead to determine the data type for a column
John,
Your reaction was what my original reaction was until I realized I had to
find out what a DEM file was and that contains enough of the kind of
depth-dimension data you describe albeit what may be a very irregular cross
section to calculate for areas and thence volumes.
If I read it correctl
Chris, since it does indeed look like homework, albeit a deeper looks
suggests it may not beI think we can safely answer the question:
>Is there any way to write codes to do this in R?
The answer is YES.
And before you ask, it can be done in Python, Java, C++, Javascript, BASIC,
FORTRAN and prob
Just FYI, the R interpreter typically saves the last value returned briefly
in a variable called .Last.value that can be accessed before you do anything
else.
> sin(.5)
[1] 0.4794255
> temp <- .Last.value
> print(temp)
[1] 0.4794255
> sin(.666)
[1] 0.6178457
> .Last.value
[1] 0.6178457
> temp
[1]
Thank you Duncan, you explained quite a bit.
I am unclear how this change causes the problem the OP mentioned.
It is an example of people using a clever trick to get what they think they
want that could be avoided if the original program provided a hook. Of
course the hook could be used more mali
With all this discussion, I shudder to ask this. I may have missed the
answers but the discussion seems to have been about identifying and solving
the problem rapidly rather than what maybe is best going forward if all
parties agree.
What was the motivation for what RSTUDIO did for their version a
This may be a dumb question and the answer may make me feel dumber.
I have had trouble for years with R packages wanting Rtools on my machine
and not being able to use it. Many packages are fine as binaries are
available. I have loaded Rtools and probably need to change my PATH or
something.
Bu
Steven,
It depends what you want to do. What you are showing seems to replace the
values stored in "data" each time.
Many kinds of loops will do that, with one simple way being to store all the
filenames in a list and loop on the contents of the list as arguments to
read.csv.
Since you show f
Nick, obviously figuring out the problem is best but you may want to deal
with the symptom.
RSTUDIO lets you adjust the sizes of the various windows and enlarging the
window (lower right normally) where the graph is shown may be a first
attempt if the problem is display space.
And note RSTUDIO
Emily,
I too copied/pasted your code in and it worked fine. I then asked for the
function definition and got it.
Did you put the entire text in? I mean nothing extra above or below except
maybe whitespace or comments?
What sometimes happens to make the code incomplete is to leave out a
matching
Having read all of the replies, it seems there are solutions for the
question and the OP points out that some solutions such as making the
document twice will affect the creation date.
I suspect the additional time to do so is seconds or at most minutes so it
may not be a big deal.
But what about
There are many techniques Callum and yours is an interesting twist I had not
considered.
Yes, you can specify what integer a factor uses to represent things but not
what I meant. Of course your trick does not work for some other forms of data
like real numbers in double format. There is a cos
There may be a point to consider about the field containing dates in the
request below. Yes, much code will "work" just fine if the column are is seen
as text as you can group by that too. The results will perhaps not be in the
order by row that you expected but you can do your re-sorting perha
Yes, Bert. At first glance I thought it was one of the merge/joins and then
wondered at the wording that made it sound like the ids may not be one per
column.
IFF the need is the simpler case, it is a straightforward enough and common
need. An example might make it clear enough so actual code c
To be fair, Jordan, I think R has some optimizations so that the arguments
in some cases are NOT evaluated until needed. So only one or the other
choice ever gets evaluated for each row. My suggestion merely has
typographic implications and some aspects of clarity and minor amounts of
less memory a
Just a minor point in the suggested solution:
df$LAP <- with(df, ifelse(G=='male', (WC-65)*TG, (WC-58)*TG))
since WC and TG are not conditional, would this be a slight improvement?
df$LAP <- with(df, TG*(WC - ifelse(G=='male', 65, 58)))
-Original Message-
From: R-help On Behalf Of Jo
Jef, your terse reply was so constructive that you converted me! LOL!
That is an interesting point though that I remain a bit unclear on.
Both data.frame and as.data.frame can be used in some ways similarly as in:
> data.frame(matrix(1:12, nrow=3))
X1 X2 X3 X4
1 1 4 7 10
2 2 5 8 11
3 3
Борис,
Try this where you tell matrix the column names you want:
nouns <- as.data.frame(
matrix(c(
"gaggle",
"geese",
"dule",
"doves",
"wake",
"vultures"
),
ncol = 2,
byrow = TRUE,
dimnames=list(NULL, c("collective", "category"
Result:
> nouns
1 - 100 of 184 matches
Mail list logo