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

Bert Gunter bgunter.4567 at gmail.com
Thu Jul 23 23:51:33 CEST 2015


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.



More information about the R-help mailing list