Skip to content
Prev 388435 / 398502 Next

concatenating columns in data.frame

I know what you mean Jeff. Yes I am very familiar with base R techniques. What I had hoped for was to do two things that some of the other methods mentioned do that ended up bringing two data.frames together as part of the solution.

Much of what I used is now standard R. I was looking at the accessory functions now commonly used in dplyr that let you dynamically select which columns to work with like begins_with() to choose. Sadly, they seem to work on a top-level but not easily within a call to something like paste(...) where they are not evaluated in the way I want.

But the odd method I tried can also be used in standard R with a bit of work. You can create a function without using dplyr that takes your df and uses it to concatenate and end with something like:

df$new_col <- do_something(df, selected_cols)

That too adds a column without the need to merge larger structures explicitly..

But your other point is a tad religious in a sense. I happen to prefer learning a core language first then looking at enhancement opportunities. But at some point, if teaching someone new who wants to focus on getting a job done simply but not necessarily repeatedly or in some ideal way, it is best to do things in a way that their mind flows better.

Many things in the tidyverse are redundant with base R or just "fix" inconsistencies like making sure the first argument is always the same. But many add substantially to doing things in a more step-by-step manner.

I do not worship the base language as it first came out or even as it has evolved. I do like to know what choices I have and pick and choose among them as needed. Of course a forum like this is more about base R than otherwise and I acknowledge that. Still, the ":=" operator is now base R. There is a new pipeline operator "|>" in base R. Some ideas, good or otherwise, do get in eventually.

I started doing graphs using base R as in the plot() command. It was adequate but I wanted better. So I learned about Lattice and various packages and eventually ggplot. I can now do things I barely imagined before and am still learning that there is much more I can do with packages underneath much of the magic and also additional packages layered above it, in some sense. So I do not approach that with an either-or mentality either.

Note I am not really talking about just R. I have similar issues with other languages I program in such as Python. None of them were created fully-formed and many had to add huge amounts to adapt to additional wants and needs. Base R for me is often inadequate. But so what?

The task being asked for in this thread in isolation, indeed may not be done any better using packages. However, if it is part of a larger set of tasks that can be pipelined, it may well be and I personally was wondering if there was a way in dplyr. There probably is a much better way than I assembled if I only knew about it, and if not, they may add this kind of indirection in a future release if deemed worthy of doing. I have gone back to programs I did years ago with humungous amounts of code using what I knew then and reducing it drastically now that I can tell a function to select say all my column names that end in .orig and apply a set of functions to them with output going to the base name followed by .mean and .sd and so on. All that can often be done in one or two lines of code where previously I had to do 18 near repetitions of each part and then another and another. That used a limited form of dynamism.

Be that as it may I think the requester has enough info and we can move on.

-----Original Message-----
From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us> 
Sent: Friday, July 2, 2021 1:03 AM
To: Avi Gross <avigross at verizon.net>; Avi Gross via R-help <r-help at r-project.org>; R-help at r-project.org
Subject: Re: [R] concatenating columns in data.frame

I use parts of the tidyverse frequently, but this post is the best argument I can imagine for learning base R techniques.
On July 1, 2021 8:41:06 PM PDT, Avi Gross via R-help <r-help at r-project.org> wrote:
--
Sent from my phone. Please excuse my brevity.