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
shift by one column given rows in a dataframe
10 messages · Massimo Bressan, Bert Gunter, Sarah Goslee +2 more
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.
Ah, so apparently you require some sort of psychic abilities... For how else would one choose which three values to keep in a row that was: a 2 b 5 based on your specification that "xxx could be anything." Cheers, 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 12:56 PM, maxbre <mbressan at arpa.veneto.it> wrote:
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.
______________________________________________ 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.
On Thu, Jul 23, 2015 at 3:56 PM, maxbre <mbressan at arpa.veneto.it> wrote:
Hi thank you for your reply: it's a neat solution but unfortunately not applicable to my specific case;
I'm going to assume you're replying to me, although there's no context whatsoever in your response (this is the R-help email list, not Nabble).
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);
Then how do you know which positions? Do you have another R object that specifies row and column number? Or do you guess? I can easily remove a random element from each row... If you aren't removing based on value, then you didn't provide a reproducible example after all, and you need to supply the index for removal. Always different? Different within a single data frame? Different between data frames? (Then you can simply change "xxx" to "whatever".) Is telepathy required? Or perhaps precognition?
thank you again for your kind help but still searching for a solution... best
Sarah Goslee http://www.functionaldiversity.org
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.
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.
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.
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:
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.
hi
thank you all for the great replies, very useful indeed even if some of them
a bit too aggressive (which is never, ever a good approach in my very humble
opinion... but that's a matter of taste and style I do not want to discuss
here); sorry again for bothering someone with such a trivial and
ill-conceived question
finally, I'm posting here my solution as a reference to the problem so that
to close this long and winding thread;
hoping this code might be somehow useful for someonelse, sometime,
somewhere...
## start code
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, stringsAsFactors = FALSE)
df_start
# set vector of rows to be shifted left
shIftLeft<-c(2,4,5)
# shift selected rows
df_start[shIftLeft,1:3]<-df_start[shIftLeft,2:4]
# final result
df_end<-df_start[,1:3]
df_end
## end code
thanks
--
View this message in context: http://r.789695.n4.nabble.com/shift-by-one-column-given-rows-in-a-dataframe-tp4710256p4710294.html
Sent from the R help mailing list archive at Nabble.com.