-1

I have the below matrix that I imported it by

data <- as.matrix(read.table("sample1.txt"), header = T)

Then I convert it as a data frame by

data.df <- as.data.frame(data)

but I cannot get a subset of this matrix by

sub3 <- subset(data.df, V1 == "General0" && V2 == "0")

this command doesn't work correctly and just select all the frame. remember that my file has different values in the first and second columns.

can you help me how can i select a subset of matrix.

         V1 V2           V3
1  General0  0 1.2946021618
2  General0  0 1.0946334452
3  General0  0 0.8449582803
4  General0  0 0.5990934855
user2312186
  • 453
  • 1
  • 4
  • 8
  • possible duplicate of [deviding a storing a file as different vectors](http://stackoverflow.com/questions/16695027/deviding-a-storing-a-file-as-different-vectors) – joran May 22 '13 at 15:57
  • i deleted that one. sorry – user2312186 May 22 '13 at 15:58
  • I want to know how can i subset a matrix – user2312186 May 22 '13 at 16:00
  • Ok, but the other question was better than this one, as it was less trivially answered by the documentation. – joran May 22 '13 at 16:00
  • see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example to learn how to post good questions (and therefore get faster and better answers) – eddi May 22 '13 at 16:09
  • I think this is clear. I don't know what is your problem? – user2312186 May 22 '13 at 16:18
  • I need to check first and second columns for each row and select the third column if the row match the conditions. – user2312186 May 22 '13 at 16:19
  • i changed the question. – user2312186 May 22 '13 at 16:46
  • Your way of reading your textfile seems a bit strange. What is wrong with `data <- read.table("sample1.txt", header=T)`? Afterwards, pleas show us `data[1:10,]`, or even better `dput(data[1:10,])`. What does `str(data)` look like? – Thilo May 22 '13 at 16:46
  • > dput(data[1:10,]) structure(c("General0", "General0", "General0", "General0", "General0", "General0", "General0", "General0", "General0", "General0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "1.2946021618", "1.0946334452", "0.8449582803", "0.5990934855", "0.3628845848", "0.1328743394", "0.0068609134", "0.0107383326", "0.0001648070", "0.0001641485"), .Dim = c(10L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"))) – user2312186 May 22 '13 at 16:50
  • when I just tried to choose one column, it works fine like: > sub3 <- subset(data.df, V2 == "0") but when I try it with two arguments, it doesn't work properly. – user2312186 May 22 '13 at 16:51
  • What did you find strange in my importing command, I have taken it from R manual. – user2312186 May 22 '13 at 16:52
  • You probably misinterpreted the point of the example in the manual. In your case, you end up converting all your numeric data to characters (or factors, depending on your default settings), which (unless you're doing something very strange) surely isn't what you wanted. – joran May 22 '13 at 16:56

1 Answers1

1

Your problem is with the &&. && is a logical operation that only works on scalar booleans (with shortcut ability, thus if the first expression is FALSE, the other expressions never are evaluated.). When doing logical expressions with vectors, just use a plain &:

sub3 <- subset(data.df, V1 == "General0" & V2 == "0")

Your import is a bit complicated. read.table returns you a perfect data.frame of your dataset. Thus, converting the output of read.table to a matrix and then reconverting it to a data.frame has only one effect. You convert all values to characters (as you have one character column) and then create a data.frame with those character values, with the result that the columns V2 and V3 are converted as factors.

If there is a valid reason to have all columns as factors, this is a valid (yet uncommon) approach. However, I can hardly imagine a use case for that. I would like

data <- read.table("sample1.txt", header = F)
sub <- subset(data, V1 == 'General0' & V2 == 0)

much better.


Edit

If you just need one column, you have at least three options (that are all well documented, by the way):

col3 <- sub3$V3

or

col3 <- with(data.df, V3[V1=='General0' & V2 == '0')

or

col3 <- data.df$V3[data.df$V1 == 'General0' & data.df&V2 == '0'])
Thilo
  • 8,827
  • 2
  • 35
  • 56
  • how can I select just the third column in sub3? – user2312186 May 22 '13 at 16:58
  • ...or the `select` argument in `subset`, also well documented. But based on the now deleted original version of @user2312186's question, none of this is really going to be a good solution to their actual problem, IMHO. – joran May 22 '13 at 17:03
  • Still, all those are techniques that are good to know. Without the original question, we will not be able to help with the actual problem. Anyway: @user2312186 if this solved your question, you can mark it as solved by clicking on the checkmark next to the answer. This way nobody else has to open the question and look at your problem. – Thilo May 22 '13 at 17:07
  • when I try to import the file like you i got this Error in eval(expr, envir, enclos) : object 'V1' not found – user2312186 May 22 '13 at 17:07
  • also, when I wanted to use col3 <- sub3$V3 I got Error in sub3$V3 : $ operator is invalid for atomic vectors do you know what is thsi exactly? – user2312186 May 22 '13 at 17:11
  • Honestly, all your problems probably become clear just by *looking* at the objects you have. Did you ever enter just `sub3` and check if it is of the form you expected it to be? What might have happened? We are glad to help you, but you also have to bring some minimal effort. Learn the basics of `R` (using one of the many tutorials out there) and you will understand what is going wrong there. Then you may be able to solve this kind of problems yourself. – Thilo May 22 '13 at 17:16