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

Sarah Goslee sarah.goslee at gmail.com
Thu Jul 23 18:27:31 CEST 2015


With one minor change to your reproducible example (thank you!):

df_start <- data.frame(v0,v1,v2,v3, stringsAsFactors=FALSE)
data.frame(t(apply(df_start, 1, function(i)i[!grepl("xxx", i)])),
stringsAsFactors=FALSE)

I'll leave it to you to deal with columns that you'd like to have
numeric. (You might also try str(df_start)).

Sarah

On Thu, Jul 23, 2015 at 12:14 PM, Massimo Bressan
<mbressan at arpa.veneto.it> wrote:
> by considering the following reproducible example:
>
> v0<-c("a","xxx","c",rep("xxx",2))
> v1<-c(1,"b",3,"d","e")
> v2<-c(6,2,8,4,5)
> v3<-c("xxx",7,"xxx",9,10)
>
> df_start<-data.frame(v0,v1,v2,v3)
> df_start
>
> v0<-letters[1:5]
> v1<-1:5
> v2<-6:10
>
> df_end<-data.frame(v0,v1,v2)
> df_end
>
> I need  to shift by one column some given rows in the initial data frame
> called "df_start" so that to get the final structure as in "df_end";
> please consider that the value "xxx" in the rows of "df_start" can be
> anything so that I necessarly need to apply by row index position (in my
> reproducible example rows: 2, 3, 5);
>
> I'm really stuck with that problem and I can not conceive any viable
> solution up to now
>
> any hints?
>
> best regards
>
> m
>
-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list