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
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
Hi thank you for your reply: it's a neat solution but unfortunately not applicable to my specific case; in fact as I specified in my first post (I may have been not enough clear, sorry for that!) I can not rely on any search method grep-like because the value "xxx" in the rows of "df_start" can be anything (string or numeric and always different) so that I necessarely need to apply by row index position (i.e. in my reproducible example rows: 2, 3, 5); thank you again for your kind help but still searching for a solution... best -- View this message in context: http://r.789695.n4.nabble.com/shift-by-one-column-given-rows-in-a-dataframe-tp4710256p4710271.html Sent from the R help mailing list archive at Nabble.com.