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

William Dunlap wdunlap at tibco.com
Thu Jul 23 23:44:22 CEST 2015


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]]



More information about the R-help mailing list