On Mar 17, 2012, at 5:27 AM, diond wrote:
> Hi,
>
> I'm an R beginner and I'm struggling with what should be a
> rudimentary task.
>
> My data is along these lines:
>
> ID name1 name2 name3 name4
> Class 0 1 0 2
> Var1 A B C A
> Var2 B C C A
> Var3 C A B A
>
> etc.
>
> I'm using the following:
>
> foo <- data.frame(t(read.table("file", header=FALSE)))
>
> but of course now it's not using ID, Class, etc. as column names.
The columns are going to be ID, name1, name2, name3, name4.
Why not do this:
foo <- t(read.table(text=" name1 name2 name3 name4
Class 0 1 0 2
Var1 A B C A
Var2 B C C A
Var3 C A B A", header=TRUE))
Class Var1 Var2 Var3
name1 "0" "A" "B" "C"
name2 "1" "B" "C" "A"
name3 "0" "C" "C" "B"
name4 "2" "A" "A" "A"
Two changes: removed the "ID" column name which had the effect of
turning the "names" into rownames during input and used header=TRUE to
"separate" the "names" from the data. You may need to coerce
to a
data.frame and you name need to coerce the Class variable to
numeric , since the result is now a character matrix,
>
> As you can imagine, I'd like to be able to use, say, foo$Var2 or foo
> $ID.
You could have used foo[ ID=="Var2", ] but that would really be a
painful approach , since you would get none of the usual help from R
functions that expect data to be column oriented>
> What's the best way to achieve this?
>
> Dion
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Reading-then-transposing-from-file-tp4480313p4480313.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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