abo dalash
2017-Apr-30 15:09 UTC
[R] Finding nrows with specefic values&converting a matrix into a table
Dear David .., Many thanks for this detailed answer. Your guidance reg. the first task has resolved my issue and I have understood now how to perform this type of analysis. I have saved your learning tips in my script. Reg. the Matrix-table conversion, could you please clarify this more?. I applied the function as.data.frame but this returned the same matrix without converting it into a list table. I'm not sure where is the problem in my code : mymatrix <- as.data.frame(mymatrix). Many thanks for your support Regards ________________________________ From: David L Carlson <dcarlson at tamu.edu> Sent: 29 April 2017 11:38 PM To: abo dalash; r-help at R-project.org Subject: RE: [R] Finding nrows with specefic values&converting a matrix into a table First. Do not use html messages, only plain text. Second. Provide a small example data set, preferably using dput(). Just printing your data can hide important information. Third. Read the documentation. Your first example does not return a logical vector at all:> dput(mydata)structure(list(Col1 = c(123L, 443L, 566L), Col2 = c(566L, 54L, 44L), Col3 = c(235L, 566L, 235L)), .Names = c("Col1", "Col2", "Col3"), class = "data.frame", row.names = c(NA, -3L))> which(mydata == 566,235)row col [1,] 3 1 [2,] 1 2 [3,] 2 3 It locates cells with 566, but not 235 which is not a surprise because you did not provide a valid logical expression to which(). There are a number of ways to get what you want, but since you want to process rows, apply() is straightforward:> Val566 <- apply(mydata, 1, function(x) any(x == 566)) > Val566[1] TRUE TRUE TRUE> Val235 <- apply(mydata, 1, function(x) any(x == 235)) > Val235[1] TRUE FALSE TRUE> which(Val235 & Val566)[1] 1 3 You should read the manual pages on any(), apply(), dput() and which() and logical expressions:> ?apply > ?any > ?dput > ?which > ?Comparison # ?"==" will also get you there.For the second question, assuming you are beginning with a table object as R defines that term and not a matrix (since all tables are matrices, but all matrices are not tables):> dput(moredata)structure(c(0L, NA, NA, 5L, 0L, NA, 67L, 23L, 0L), .Dim = c(3L, 3L), .Dimnames = list(c("x", "y", "z"), c("x", "y", "z")), class = "table")> moredatax y z x 0 5 67 y NA 0 23 z NA NA 0 Note, that your example uses na rather than NA. R is case sensitive so na is just an ordinary character string while NA is a missing value indicator. This is one of the reasons that dput() is important> moredata.df <- as.data.frame(moredata) > moredata.dfVar1 Var2 Freq 1 x x 0 2 y x NA 3 z x NA 4 x y 5 5 y y 0 6 z y NA 7 x z 67 8 y z 23 9 z z 0> moredata.df[order(moredata.df$Freq, decreasing=TRUE), ]Var1 Var2 Freq 7 x z 67 8 y z 23 4 x y 5 1 x x 0 5 y y 0 9 z z 0 2 y x NA 3 z x NA 6 z y NA For this you should read the following manual pages:> ?as.data.frame > ?order > ?Extract------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of abo dalash Sent: Saturday, April 29, 2017 10:14 AM To: r-help at R-project.org Subject: [R] Finding nrows with specefic values&converting a matrix into a table Hi All I'm trying to identify number of rows containing 2 specific values. I tried : which(mydata == 566,235), but this returns logical values for all rows and any T in a certain row indicates the existence of one of these values but what I need to know is only number of rows in my data set with these 2 particular values considering these two values as one pair per column. For example : 1 123 566 235 2 443 54 566 3 566 44 235 here number of rows with the values 566&235 is 2 which are rows 1 & 3. Row 2 has only 566 so it should not be included in our calculation. I also have a large matrix and wanted to convert it into a table so I can easily identify the combination with higher frequencies. The matrix looks like this: x y z x 0 5 67 y na 0 23 z na na 0 and I would like to convert this into a table arranged with higher values first like this : x z 67 y z 23 x y 5 x x 0 y y 0 z z 0 y x na z x na z y na Is there simple function to perform this conversion with some explanation about the Syntax Regards [[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. [[alternative HTML version deleted]]
David L Carlson
2017-Apr-30 15:47 UTC
[R] Finding nrows with specefic values&converting a matrix into a table
You did not give me any information about about your data using str() or class() so I'll guess that you have a matrix, e.g.:> class(moredata)[1] "matrix"> as.data.frame.table(moredata)Var1 Var2 Freq 1 x x 0 2 y x NA 3 z x NA 4 x y 5 5 y y 0 6 z y NA 7 x z 67 8 y z 23 9 z z 0 David C From: abo dalash [mailto:abo_dlsh at hotmail.com] Sent: Sunday, April 30, 2017 10:09 AM To: David L Carlson <dcarlson at tamu.edu>; r-help at R-project.org Subject: Re: [R] Finding nrows with specefic values&converting a matrix into a table Dear David .., Many thanks for this detailed answer. Your guidance reg. the first task has resolved my issue and I have understood now how to perform this type of analysis. I have saved your learning tips in my script. Reg. the Matrix-table conversion, could you please clarify this?more?. I applied the function as.data.frame but this returned the same matrix without converting it into a list table. I'm not sure where is the problem in my code :???mymatrix <- as.data.frame(mymatrix). Many thanks for your support Regards ________________________________________ From: David L Carlson <dcarlson at tamu.edu> Sent: 29 April 2017 11:38 PM To: abo dalash; r-help at R-project.org Subject: RE: [R] Finding nrows with specefic values&converting a matrix into a table ? First. Do not use html messages, only plain text. Second. Provide a small example data set, preferably using dput(). Just printing your data can hide important information. Third. Read the documentation. Your first example does not return a logical vector at all:> dput(mydata)structure(list(Col1 = c(123L, 443L, 566L), Col2 = c(566L, 54L, 44L), Col3 = c(235L, 566L, 235L)), .Names = c("Col1", "Col2", "Col3"), class = "data.frame", row.names = c(NA, -3L))> which(mydata == 566,235)???? row col [1,]?? 3?? 1 [2,]?? 1?? 2 [3,]?? 2?? 3 It locates cells with 566, but not 235 which is not a surprise because you did not provide a valid logical expression to which(). There are a number of ways to get what you want, but since you want to process rows, apply() is straightforward:> Val566 <- apply(mydata, 1, function(x) any(x == 566)) > Val566[1] TRUE TRUE TRUE> Val235 <- apply(mydata, 1, function(x) any(x == 235)) > Val235[1]? TRUE FALSE? TRUE> which(Val235 & Val566)[1] 1 3 You should read the manual pages on any(), apply(), dput() and which() and logical expressions:> ?apply > ?any > ?dput > ?which > ?Comparison # ?"==" will also get you there.For the second question, assuming you are beginning with a table object as R defines that term and not a matrix (since all tables are matrices, but all matrices are not tables):> dput(moredata)structure(c(0L, NA, NA, 5L, 0L, NA, 67L, 23L, 0L), .Dim = c(3L, 3L), .Dimnames = list(c("x", "y", "z"), c("x", "y", "z")), class = "table")> moredata?? x? y? z x? 0? 5 67 y NA? 0 23 z NA NA? 0 Note, that your example uses na rather than NA. R is case sensitive so na is just an ordinary character string while NA is a missing value indicator. This is one of the reasons that dput() is important> moredata.df <- as.data.frame(moredata) > moredata.df? Var1 Var2 Freq 1??? x??? x??? 0 2??? y??? x?? NA 3??? z??? x?? NA 4??? x??? y??? 5 5??? y??? y??? 0 6??? z??? y?? NA 7??? x??? z?? 67 8??? y??? z?? 23 9??? z??? z??? 0> moredata.df[order(moredata.df$Freq, decreasing=TRUE), ]? Var1 Var2 Freq 7??? x??? z?? 67 8??? y??? z?? 23 4??? x??? y??? 5 1??? x??? x??? 0 5??? y??? y??? 0 9??? z??? z??? 0 2??? y??? x?? NA 3??? z??? x?? NA 6??? z??? y?? NA For this you should read the following manual pages:> ?as.data.frame > ?order > ?Extract------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of abo dalash Sent: Saturday, April 29, 2017 10:14 AM To: r-help at R-project.org Subject: [R] Finding nrows with specefic values&converting a matrix into a table Hi All I'm trying to identify number of rows containing 2 specific values. I tried : which(mydata == 566,235), but this returns logical values for all rows and any T in a certain row indicates the existence of one of these values but what I need to know is only number of rows in my data set with these 2 particular values considering these two values as one pair per column. For example : 1????????? 123?? 566??? 235 2????????? 443??? 54????? 566 3????????? 566??? 44????? 235 here number of rows with the values 566&235 is 2 which are rows 1 & 3. Row 2 has only 566 so it should not be included in our calculation. I also have a large matrix and wanted to convert it into a table so I can easily identify the combination with higher frequencies. The matrix looks like this: ??????????????????? x????? y????? z x????????????????? 0????? 5?????? 67 y????????????????? na??? 0????? 23 z?????????????????? na?? na????? 0 and I would like to convert this into a table arranged with higher values first like this : x?????? z?????? 67 y?????? z?????? 23 x?????? y??????? 5 x?????? x??????? 0 y?????? y??????? 0 z??????? z??????? 0 y??????? x??????? na z??????? x??????? na z??????? y??????? na Is there simple function to perform this conversion with some explanation about the Syntax Regards ??????? [[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.
David L Carlson
2017-Apr-30 16:33 UTC
[R] Finding nrows with specefic values&converting a matrix into a table
Show us the code you used. Don't just tell us what you did. It is likely that something you did after creating the matrix converted it to a data frame. Copy and paste your code to your emails.> str(mydf)'data.frame': 3 obs. of 3 variables: $ x: int 0 NA NA $ y: int 5 0 NA $ z: int 67 23 0> data.frame(as.table(as.matrix(mydf)))Var1 Var2 Freq 1 x x 0 2 y x NA 3 z x NA 4 x y 5 5 y y 0 6 z y NA David C From: abo dalash [mailto:abo_dlsh at hotmail.com] Sent: Sunday, April 30, 2017 11:13 AM To: David L Carlson <dcarlson at tamu.edu> Subject: Re: [R] Finding nrows with specefic values&converting a matrix into a table str(mymatrix) The Structure shows that this is a 'data.frame'?? of 120 obs. and 120 variables of numeric type. R deals with my matrix as a data frame although I used the function matrix() to produce this matrix which is not clear to me why. As this is already a data.frame, this may explains why R returns me the same matrix. What do you recommend now? Many thanks ________________________________________ From: David L Carlson <dcarlson at tamu.edu> Sent: 30 April 2017 06:47 PM To: abo dalash; r-help at R-project.org Subject: RE: [R] Finding nrows with specefic values&converting a matrix into a table ? You did not give me any information about about your data using str() or class() so I'll guess that you have a matrix, e.g.:> class(moredata)[1] "matrix"> as.data.frame.table(moredata)? Var1 Var2 Freq 1??? x??? x??? 0 2??? y??? x?? NA 3??? z??? x?? NA 4??? x??? y??? 5 5??? y??? y??? 0 6??? z??? y?? NA 7??? x??? z?? 67 8??? y??? z?? 23 9??? z??? z??? 0 David C From: abo dalash [mailto:abo_dlsh at hotmail.com] Sent: Sunday, April 30, 2017 10:09 AM To: David L Carlson <dcarlson at tamu.edu>; r-help at R-project.org Subject: Re: [R] Finding nrows with specefic values&converting a matrix into a table Dear David .., Many thanks for this detailed answer. Your guidance reg. the first task has resolved my issue and I have understood now how to perform this type of analysis. I have saved your learning tips in my script. Reg. the Matrix-table conversion, could you please clarify this?more?. I applied the function as.data.frame but this returned the same matrix without converting it into a list table. I'm not sure where is the problem in my code :???mymatrix <- as.data.frame(mymatrix). Many thanks for your support Regards ________________________________________ From: David L Carlson <dcarlson at tamu.edu> Sent: 29 April 2017 11:38 PM To: abo dalash; r-help at R-project.org Subject: RE: [R] Finding nrows with specefic values&converting a matrix into a table ? First. Do not use html messages, only plain text. Second. Provide a small example data set, preferably using dput(). Just printing your data can hide important information. Third. Read the documentation. Your first example does not return a logical vector at all:> dput(mydata)structure(list(Col1 = c(123L, 443L, 566L), Col2 = c(566L, 54L, 44L), Col3 = c(235L, 566L, 235L)), .Names = c("Col1", "Col2", "Col3"), class = "data.frame", row.names = c(NA, -3L))> which(mydata == 566,235)???? row col [1,]?? 3?? 1 [2,]?? 1?? 2 [3,]?? 2?? 3 It locates cells with 566, but not 235 which is not a surprise because you did not provide a valid logical expression to which(). There are a number of ways to get what you want, but since you want to process rows, apply() is straightforward:> Val566 <- apply(mydata, 1, function(x) any(x == 566)) > Val566[1] TRUE TRUE TRUE> Val235 <- apply(mydata, 1, function(x) any(x == 235)) > Val235[1]? TRUE FALSE? TRUE> which(Val235 & Val566)[1] 1 3 You should read the manual pages on any(), apply(), dput() and which() and logical expressions:> ?apply > ?any > ?dput > ?which > ?Comparison # ?"==" will also get you there.For the second question, assuming you are beginning with a table object as R defines that term and not a matrix (since all tables are matrices, but all matrices are not tables):> dput(moredata)structure(c(0L, NA, NA, 5L, 0L, NA, 67L, 23L, 0L), .Dim = c(3L, 3L), .Dimnames = list(c("x", "y", "z"), c("x", "y", "z")), class = "table")> moredata?? x? y? z x? 0? 5 67 y NA? 0 23 z NA NA? 0 Note, that your example uses na rather than NA. R is case sensitive so na is just an ordinary character string while NA is a missing value indicator. This is one of the reasons that dput() is important> moredata.df <- as.data.frame(moredata) > moredata.df? Var1 Var2 Freq 1??? x??? x??? 0 2??? y??? x?? NA 3??? z??? x?? NA 4??? x??? y??? 5 5??? y??? y??? 0 6??? z??? y?? NA 7??? x??? z?? 67 8??? y??? z?? 23 9??? z??? z??? 0> moredata.df[order(moredata.df$Freq, decreasing=TRUE), ]? Var1 Var2 Freq 7??? x??? z?? 67 8??? y??? z?? 23 4??? x??? y??? 5 1??? x??? x??? 0 5??? y??? y??? 0 9??? z??? z??? 0 2??? y??? x?? NA 3??? z??? x?? NA 6??? z??? y?? NA For this you should read the following manual pages:> ?as.data.frame > ?order > ?Extract------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of abo dalash Sent: Saturday, April 29, 2017 10:14 AM To: r-help at R-project.org Subject: [R] Finding nrows with specefic values&converting a matrix into a table Hi All I'm trying to identify number of rows containing 2 specific values. I tried : which(mydata == 566,235), but this returns logical values for all rows and any T in a certain row indicates the existence of one of these values but what I need to know is only number of rows in my data set with these 2 particular values considering these two values as one pair per column. For example : 1????????? 123?? 566??? 235 2????????? 443??? 54????? 566 3????????? 566??? 44????? 235 here number of rows with the values 566&235 is 2 which are rows 1 & 3. Row 2 has only 566 so it should not be included in our calculation. I also have a large matrix and wanted to convert it into a table so I can easily identify the combination with higher frequencies. The matrix looks like this: ??????????????????? x????? y????? z x????????????????? 0????? 5?????? 67 y????????????????? na??? 0????? 23 z?????????????????? na?? na????? 0 and I would like to convert this into a table arranged with higher values first like this : x?????? z?????? 67 y?????? z?????? 23 x?????? y??????? 5 x?????? x??????? 0 y?????? y??????? 0 z??????? z??????? 0 y??????? x??????? na z??????? x??????? na z??????? y??????? na Is there simple function to perform this conversion with some explanation about the Syntax Regards ??????? [[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.
abo dalash
2017-Apr-30 17:17 UTC
[R] Finding nrows with specefic values&converting a matrix into a table
Many thanks dear David, your teaching reg. the conversion of a matrix into a table has resolved my issue and I have now generated the desired table. So many thanks for your support. Regards ________________________________ From: David L Carlson <dcarlson at tamu.edu> Sent: 30 April 2017 07:33 PM To: abo dalash; r-help at R-project.org Subject: RE: [R] Finding nrows with specefic values&converting a matrix into a table Show us the code you used. Don't just tell us what you did. It is likely that something you did after creating the matrix converted it to a data frame. Copy and paste your code to your emails.> str(mydf)'data.frame': 3 obs. of 3 variables: $ x: int 0 NA NA $ y: int 5 0 NA $ z: int 67 23 0> data.frame(as.table(as.matrix(mydf)))Var1 Var2 Freq 1 x x 0 2 y x NA 3 z x NA 4 x y 5 5 y y 0 6 z y NA David C From: abo dalash [mailto:abo_dlsh at hotmail.com] Sent: Sunday, April 30, 2017 11:13 AM To: David L Carlson <dcarlson at tamu.edu> Subject: Re: [R] Finding nrows with specefic values&converting a matrix into a table str(mymatrix) The Structure shows that this is a 'data.frame'? of 120 obs. and 120 variables of numeric type. R deals with my matrix as a data frame although I used the function matrix() to produce this matrix which is not clear to me why. As this is already a data.frame, this may explains why R returns me the same matrix. What do you recommend now? Many thanks ________________________________________ From: David L Carlson <dcarlson at tamu.edu> Sent: 30 April 2017 06:47 PM To: abo dalash; r-help at R-project.org Subject: RE: [R] Finding nrows with specefic values&converting a matrix into a table You did not give me any information about about your data using str() or class() so I'll guess that you have a matrix, e.g.:> class(moredata)[1] "matrix"> as.data.frame.table(moredata)Var1 Var2 Freq 1 x x 0 2 y x NA 3 z x NA 4 x y 5 5 y y 0 6 z y NA 7 x z 67 8 y z 23 9 z z 0 David C From: abo dalash [mailto:abo_dlsh at hotmail.com] Sent: Sunday, April 30, 2017 10:09 AM To: David L Carlson <dcarlson at tamu.edu>; r-help at R-project.org Subject: Re: [R] Finding nrows with specefic values&converting a matrix into a table Dear David .., Many thanks for this detailed answer. Your guidance reg. the first task has resolved my issue and I have understood now how to perform this type of analysis. I have saved your learning tips in my script. Reg. the Matrix-table conversion, could you please clarify this more?. I applied the function as.data.frame but this returned the same matrix without converting it into a list table. I'm not sure where is the problem in my code : mymatrix <- as.data.frame(mymatrix). Many thanks for your support Regards ________________________________________ From: David L Carlson <dcarlson at tamu.edu> Sent: 29 April 2017 11:38 PM To: abo dalash; r-help at R-project.org Subject: RE: [R] Finding nrows with specefic values&converting a matrix into a table First. Do not use html messages, only plain text. Second. Provide a small example data set, preferably using dput(). Just printing your data can hide important information. Third. Read the documentation. Your first example does not return a logical vector at all:> dput(mydata)structure(list(Col1 = c(123L, 443L, 566L), Col2 = c(566L, 54L, 44L), Col3 = c(235L, 566L, 235L)), .Names = c("Col1", "Col2", "Col3"), class = "data.frame", row.names = c(NA, -3L))> which(mydata == 566,235)row col [1,] 3 1 [2,] 1 2 [3,] 2 3 It locates cells with 566, but not 235 which is not a surprise because you did not provide a valid logical expression to which(). There are a number of ways to get what you want, but since you want to process rows, apply() is straightforward:> Val566 <- apply(mydata, 1, function(x) any(x == 566)) > Val566[1] TRUE TRUE TRUE> Val235 <- apply(mydata, 1, function(x) any(x == 235)) > Val235[1] TRUE FALSE TRUE> which(Val235 & Val566)[1] 1 3 You should read the manual pages on any(), apply(), dput() and which() and logical expressions:> ?apply > ?any > ?dput > ?which > ?Comparison # ?"==" will also get you there.For the second question, assuming you are beginning with a table object as R defines that term and not a matrix (since all tables are matrices, but all matrices are not tables):> dput(moredata)structure(c(0L, NA, NA, 5L, 0L, NA, 67L, 23L, 0L), .Dim = c(3L, 3L), .Dimnames = list(c("x", "y", "z"), c("x", "y", "z")), class = "table")> moredatax y z x 0 5 67 y NA 0 23 z NA NA 0 Note, that your example uses na rather than NA. R is case sensitive so na is just an ordinary character string while NA is a missing value indicator. This is one of the reasons that dput() is important> moredata.df <- as.data.frame(moredata) > moredata.dfVar1 Var2 Freq 1 x x 0 2 y x NA 3 z x NA 4 x y 5 5 y y 0 6 z y NA 7 x z 67 8 y z 23 9 z z 0> moredata.df[order(moredata.df$Freq, decreasing=TRUE), ]Var1 Var2 Freq 7 x z 67 8 y z 23 4 x y 5 1 x x 0 5 y y 0 9 z z 0 2 y x NA 3 z x NA 6 z y NA For this you should read the following manual pages:> ?as.data.frame > ?order > ?Extract------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of abo dalash Sent: Saturday, April 29, 2017 10:14 AM To: r-help at R-project.org Subject: [R] Finding nrows with specefic values&converting a matrix into a table Hi All I'm trying to identify number of rows containing 2 specific values. I tried : which(mydata == 566,235), but this returns logical values for all rows and any T in a certain row indicates the existence of one of these values but what I need to know is only number of rows in my data set with these 2 particular values considering these two values as one pair per column. For example : 1 123 566 235 2 443 54 566 3 566 44 235 here number of rows with the values 566&235 is 2 which are rows 1 & 3. Row 2 has only 566 so it should not be included in our calculation. I also have a large matrix and wanted to convert it into a table so I can easily identify the combination with higher frequencies. The matrix looks like this: x y z x 0 5 67 y na 0 23 z na na 0 and I would like to convert this into a table arranged with higher values first like this : x z 67 y z 23 x y 5 x x 0 y y 0 z z 0 y x na z x na z y na Is there simple function to perform this conversion with some explanation about the Syntax Regards [[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. [[alternative HTML version deleted]]
abo dalash
2017-Apr-30 19:35 UTC
[R] Finding nrows with specefic values&converting a matrix into a table
I'm trying to write the table I have created from the matrix using write.table(mytable, file= "mytable.txt"). I have imported this txt. file into an Excel sheet but all data have been typed in one column (Var1,Var2,&Freq.) and I want to see each vector in one column. Have I used the correct syntax? Regards ________________________________ From: David L Carlson <dcarlson at tamu.edu> Sent: 30 April 2017 07:33 PM To: abo dalash; r-help at R-project.org Subject: RE: [R] Finding nrows with specefic values&converting a matrix into a table Show us the code you used. Don't just tell us what you did. It is likely that something you did after creating the matrix converted it to a data frame. Copy and paste your code to your emails.> str(mydf)'data.frame': 3 obs. of 3 variables: $ x: int 0 NA NA $ y: int 5 0 NA $ z: int 67 23 0> data.frame(as.table(as.matrix(mydf)))Var1 Var2 Freq 1 x x 0 2 y x NA 3 z x NA 4 x y 5 5 y y 0 6 z y NA David C From: abo dalash [mailto:abo_dlsh at hotmail.com] Sent: Sunday, April 30, 2017 11:13 AM To: David L Carlson <dcarlson at tamu.edu> Subject: Re: [R] Finding nrows with specefic values&converting a matrix into a table str(mymatrix) The Structure shows that this is a 'data.frame'? of 120 obs. and 120 variables of numeric type. R deals with my matrix as a data frame although I used the function matrix() to produce this matrix which is not clear to me why. As this is already a data.frame, this may explains why R returns me the same matrix. What do you recommend now? Many thanks ________________________________________ From: David L Carlson <dcarlson at tamu.edu> Sent: 30 April 2017 06:47 PM To: abo dalash; r-help at R-project.org Subject: RE: [R] Finding nrows with specefic values&converting a matrix into a table You did not give me any information about about your data using str() or class() so I'll guess that you have a matrix, e.g.:> class(moredata)[1] "matrix"> as.data.frame.table(moredata)Var1 Var2 Freq 1 x x 0 2 y x NA 3 z x NA 4 x y 5 5 y y 0 6 z y NA 7 x z 67 8 y z 23 9 z z 0 David C From: abo dalash [mailto:abo_dlsh at hotmail.com] Sent: Sunday, April 30, 2017 10:09 AM To: David L Carlson <dcarlson at tamu.edu>; r-help at R-project.org Subject: Re: [R] Finding nrows with specefic values&converting a matrix into a table Dear David .., Many thanks for this detailed answer. Your guidance reg. the first task has resolved my issue and I have understood now how to perform this type of analysis. I have saved your learning tips in my script. Reg. the Matrix-table conversion, could you please clarify this more?. I applied the function as.data.frame but this returned the same matrix without converting it into a list table. I'm not sure where is the problem in my code : mymatrix <- as.data.frame(mymatrix). Many thanks for your support Regards ________________________________________ From: David L Carlson <dcarlson at tamu.edu> Sent: 29 April 2017 11:38 PM To: abo dalash; r-help at R-project.org Subject: RE: [R] Finding nrows with specefic values&converting a matrix into a table First. Do not use html messages, only plain text. Second. Provide a small example data set, preferably using dput(). Just printing your data can hide important information. Third. Read the documentation. Your first example does not return a logical vector at all:> dput(mydata)structure(list(Col1 = c(123L, 443L, 566L), Col2 = c(566L, 54L, 44L), Col3 = c(235L, 566L, 235L)), .Names = c("Col1", "Col2", "Col3"), class = "data.frame", row.names = c(NA, -3L))> which(mydata == 566,235)row col [1,] 3 1 [2,] 1 2 [3,] 2 3 It locates cells with 566, but not 235 which is not a surprise because you did not provide a valid logical expression to which(). There are a number of ways to get what you want, but since you want to process rows, apply() is straightforward:> Val566 <- apply(mydata, 1, function(x) any(x == 566)) > Val566[1] TRUE TRUE TRUE> Val235 <- apply(mydata, 1, function(x) any(x == 235)) > Val235[1] TRUE FALSE TRUE> which(Val235 & Val566)[1] 1 3 You should read the manual pages on any(), apply(), dput() and which() and logical expressions:> ?apply > ?any > ?dput > ?which > ?Comparison # ?"==" will also get you there.For the second question, assuming you are beginning with a table object as R defines that term and not a matrix (since all tables are matrices, but all matrices are not tables):> dput(moredata)structure(c(0L, NA, NA, 5L, 0L, NA, 67L, 23L, 0L), .Dim = c(3L, 3L), .Dimnames = list(c("x", "y", "z"), c("x", "y", "z")), class = "table")> moredatax y z x 0 5 67 y NA 0 23 z NA NA 0 Note, that your example uses na rather than NA. R is case sensitive so na is just an ordinary character string while NA is a missing value indicator. This is one of the reasons that dput() is important> moredata.df <- as.data.frame(moredata) > moredata.dfVar1 Var2 Freq 1 x x 0 2 y x NA 3 z x NA 4 x y 5 5 y y 0 6 z y NA 7 x z 67 8 y z 23 9 z z 0> moredata.df[order(moredata.df$Freq, decreasing=TRUE), ]Var1 Var2 Freq 7 x z 67 8 y z 23 4 x y 5 1 x x 0 5 y y 0 9 z z 0 2 y x NA 3 z x NA 6 z y NA For this you should read the following manual pages:> ?as.data.frame > ?order > ?Extract------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of abo dalash Sent: Saturday, April 29, 2017 10:14 AM To: r-help at R-project.org Subject: [R] Finding nrows with specefic values&converting a matrix into a table Hi All I'm trying to identify number of rows containing 2 specific values. I tried : which(mydata == 566,235), but this returns logical values for all rows and any T in a certain row indicates the existence of one of these values but what I need to know is only number of rows in my data set with these 2 particular values considering these two values as one pair per column. For example : 1 123 566 235 2 443 54 566 3 566 44 235 here number of rows with the values 566&235 is 2 which are rows 1 & 3. Row 2 has only 566 so it should not be included in our calculation. I also have a large matrix and wanted to convert it into a table so I can easily identify the combination with higher frequencies. The matrix looks like this: x y z x 0 5 67 y na 0 23 z na na 0 and I would like to convert this into a table arranged with higher values first like this : x z 67 y z 23 x y 5 x x 0 y y 0 z z 0 y x na z x na z y na Is there simple function to perform this conversion with some explanation about the Syntax Regards [[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. [[alternative HTML version deleted]]