[R] shift by one column given rows in a dataframe

Marissa Fahlberg marissa.fahlberg at gmail.com
Fri Jul 24 02:18:43 CEST 2015


Turns out my column and row names were too long to fit in the console and
when I widened the window the table didn't automatically adjust and widen
as well. I had to widen the console first and then retype the same command
so it fit. Silly mistake. I usually keep the console pretty narrow on my
laptop and never considered that would change the format.

Thanks for all the help - I'm sure I'll be back again very soon with more
questions!!
On Jul 23, 2015 4:53 PM, "Bert Gunter" <bgunter.4567 at gmail.com> wrote:

> Oops, Bill's reply and mine crossed in the email. His is essentially
> the same as mine except probably more efficient.
>
>
> -- Bert
> Bert Gunter
>
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
>    -- Clifford Stoll
>
>
> On Thu, Jul 23, 2015 at 2:44 PM, William Dunlap <wdunlap at tibco.com> wrote:
> > You could do something like the following
> >   > rowsToShiftLeft <- c(2,4,5) # 4, not the 3 that was in the original
> post
> >   > mat <- as.matrix(df_start)
> >   > mat[rowsToShiftLeft, 1:3] <- mat[rowsToShiftLeft, 2:4]
> >   > result <- data.frame(mat[, 1:3], stringsAsFactors=FALSE)
> >   > str(result)
> >   'data.frame':   5 obs. of  3 variables:
> >    $ v0: chr  "a" "b" "c" "d" ...
> >    $ v1: chr  "1" "2" "3" "4" ...
> >    $ v2: chr  "6" "7" "8" "9" ...
> > You will then have to convert the columns which ought to be numeric
> > to numeric.  (All the columns in df_start were factors because of the
> > extra xxx that offset some of them.)
> >
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com
> >
> > On Thu, Jul 23, 2015 at 1:19 PM, maxbre <mbressan at arpa.veneto.it> wrote:
> >
> >> sorry but honestly I do not get your point
> >>
> >> I need to shift to left by one position (i.e. one column) the entire
> rows
> >> 2,4,5 of "df_start" so that to obtain as final result the structure
> >> indicated in "df_end"
> >>
> >> I know in advance the rows that I need to shift
> >>
> >> hope it clears a bit, now
> >>
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >>
> http://r.789695.n4.nabble.com/shift-by-one-column-given-rows-in-a-dataframe-tp4710256p4710276.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list