arun
2013-Sep-20 16:11 UTC
[R] Compare two subsequent rows based on specific values of a string
Hi,
May be this helps:
dat1<- read.table(text="x1??? x2??? x3?? x4?
1???? xz?? ab??? cd??? ef
2???? ab?? fz??? cd??? ef
3???? ab?? cd?? dy???
dx",sep="",header=TRUE,stringsAsFactors=FALSE)
dat1$changes_to_row_above<- sapply(seq_len(nrow(dat1)),function(i)
{x1<-dat1[,i]%in% dat1[,i-1];if(any(x1)) sum(x1,na.rm=TRUE) else NA})
?dat1
#? x1 x2 x3 x4 changes_to_row_above
#1 xz ab cd ef?????????????????? NA
#2 ab fz cd ef??????????????????? 1
#3 ab cd dy dx??????????????????? 2
A.K.
Dear all,
I would like to compare two rows and check whether something
changed. The only thing is, it is not always the same column that needs
to be compared. It is rather a matrix of values. The outcome would be a
number of changes in these, for instance: "of row 2, there are 3 exactly
same values in row 3". I will make up an example:
? ? ? x1 ? ?x2 ? ?x3 ? x4 ? changes_to_row_above
1 ? ? xz ? ab ? ?cd ? ?ef ? NA
2 ? ? ab ? fz ? ?cd ? ?ef ? 1
3 ? ? ab ? cd ? dy ? ?dx ? 2
and so forth...
Do you think that would be possible? Is there an easy function
to do so? It would be great help guys, thanks a lot in advance, sorry
for my bad data manipulation knowledge...
Tobi
Michel
2013-Sep-20 16:37 UTC
[R] Compare two subsequent rows based on specific values of a string
Thanks I'm lookin for yur example
-----Message d'origine-----
De?: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] De
la part de arun
Envoy??: vendredi 20 septembre 2013 18:11
??: R help
Objet?: Re: [R] Compare two subsequent rows based on specific values of a
string
Hi,
May be this helps:
dat1<- read.table(text="x1??? x2??? x3?? x4
1???? xz?? ab??? cd??? ef
2???? ab?? fz??? cd??? ef
3???? ab?? cd?? dy???
dx",sep="",header=TRUE,stringsAsFactors=FALSE)
dat1$changes_to_row_above<- sapply(seq_len(nrow(dat1)),function(i)
{x1<-dat1[,i]%in% dat1[,i-1];if(any(x1)) sum(x1,na.rm=TRUE) else NA})
?dat1
#? x1 x2 x3 x4 changes_to_row_above
#1 xz ab cd ef?????????????????? NA
#2 ab fz cd ef??????????????????? 1
#3 ab cd dy dx??????????????????? 2
A.K.
Dear all,
I would like to compare two rows and check whether something changed. The
only thing is, it is not always the same column that needs to be compared.
It is rather a matrix of values. The outcome would be a number of changes in
these, for instance: "of row 2, there are 3 exactly same values in row
3".
I will make up an example:
? ? ? x1 ? ?x2 ? ?x3 ? x4 ? changes_to_row_above
1 ? ? xz ? ab ? ?cd ? ?ef ? NA
2 ? ? ab ? fz ? ?cd ? ?ef ? 1
3 ? ? ab ? cd ? dy ? ?dx ? 2
and so forth...
Do you think that would be possible? Is there an easy function to do so? It
would be great help guys, thanks a lot in advance, sorry for my bad data
manipulation knowledge...
Tobi
______________________________________________
R-help at r-project.org mailing list
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.