On Oct 3, 2010, at 10:22 PM, Gundala Viswanath wrote:

I have a data frame that looks like this:


print(df)
V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 1 FN 8.637 28.890 31.430 31.052 29.878 33.215 32.728 32.187 29.305 31.462 2 FP 19.936 30.284 33.001 35.100 30.238 34.452 35.849 34.185 31.242 35.635 3 TN 0.000 17.190 16.460 21.100 17.960 15.120 17.200 17.190 15.270 15.310 4 TP 22.831 31.246 33.600 35.439 32.073 33.947 35.050 34.472 31.228 33.701


How can I extract rows as specified, e.g.
I tried this to extract the first line ("FN") starting from V3 to V12:

fn <- df[1,df$V3:df$V12]

But it gives columns starting not from V3.

The ":" operator only works for numeric values in [,] or []. And even then you would have been passing a very strange arguemtn to ":", since df$V3 is a vector rather than a scalar. But these would also fail:
df[1, V3:V12]
df[1, "V3":"V12"]


These all work:

df[which(df$V2=="FN"), grep("^V3$", names(df)):grep("^V12$", names(df)) ]

df[1, 2:11]

df[1, -1]


# the subset function may have tricked you into believing that my statement about the ":" operator was false, but that function first parses the select= (and subset=) expressions against column names and returns column numbers before passing to ":"

subset(df, V2=="FN", select=-V2)

subset(df, V2=="FN", select=V3:V12)


What's the right way to do it?

- G.V.

______________________________________________
R-help@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.

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to