Sorry, but I'm very new to R. I'm trying to figure out how to convert a
column from a data frame to a vector. More specifically, I have read in a
comma separated value table which contains a number of variables in columns,
plots in rows...
t <- read.table("G:/R/table1.txt", sep=",", header=TRUE)
the data comes in just fine. Now I want to do an XY plot of 2 variables and
will be doing some statistical analyses on them. But
plot(t["Var1"], t["Var2"])
Gives me the error, "Error in pmatch(x, table, duplicates.ok) :
argument is not of mode character"
and
v1 <- t["Var1"]
v2 <- t["Var2"]
plot(v1, v2)
gives me the same error (of course) and v1 does not display the same as a
vector; it looks like it is an array(?)
Thanks,
-Eric
---
Eric B. Peterson
Vegetation Ecologist & Lichenologist
Nevada Natural Heritage Program
Department of Conservation and Natural Resources
1550 E. College Pkwy #137
Carson City, NV 89706
775-687-4245 x236
http://heritage.nv.gov/
On Tuesday 21 January 2003 03:47 pm, Eric Peterson wrote:> Sorry, but I'm very new to R. I'm trying to figure out how to convert a > column from a data frame to a vector. More specifically, I have read in a > comma separated value table which contains a number of variables in > columns, plots in rows... > > t <- read.table("G:/R/table1.txt", sep=",", header=TRUE) > > the data comes in just fine. Now I want to do an XY plot of 2 variables > and will be doing some statistical analyses on them. But > > plot(t["Var1"], t["Var2"])You want t$Var1 instead of t["Var1"], etc. t, incidentally, is the name of an R function, so you might want to avoid using it as a variable name. Deepayan
Hi, ----- Original Message ----- From: "Eric Peterson" <peterson at heritage.nv.gov> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, January 22, 2003 10:47 AM Subject: [R] newbie: data frame to vector> Sorry, but I'm very new to R. I'm trying to figure out how to convert a > column from a data frame to a vector. More specifically, I have read in a > comma separated value table which contains a number of variables incolumns,> plots in rows... > > t <- read.table("G:/R/table1.txt", sep=",", header=TRUE) > > the data comes in just fine. Now I want to do an XY plot of 2 variablesand> will be doing some statistical analyses on them. But > > plot(t["Var1"], t["Var2"])I'm assuming your data frame, t, looks like: Var1 Var2 Var3 1 2 3 4 5 6 7 8 9 Then there are (at least) two ways. The first is type: attach(t) right after your > t <- read.table("G:/R/table1.txt", sep=",", header=TRUE) this will allow you to use the column names directly, i.e. you can do: plot(Var1, Var2) If you do not attach it, you can still achieve your goal. Remember that the data frame is two dimensional, to subset a two dimensional object you need a comma, i.e t[1, ] # give you row 1 t[, 1] # give you column 1 t[1, 1] # give you the first value in row 1, column 1 So to do what you want you need: plot(t[, 1], t[, 2]) # Plot Column 1 and 2 Hope this helps, Kevin Wang ------------------------------------------------ Ko-Kang Kevin Wang Master of Science (MSc) Student Department of Statistics University of Auckland New Zealand www.stat.auckland.ac.nz/~kwan022