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'])