Reading then transposing from file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Reading then transposing from file

diond
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.

As you can imagine, I'd like to be able to use, say, foo$Var2 or foo$ID.

What's the best way to achieve this?

Dion
Reply | Threaded
Open this post in threaded view
|

Re: Reading then transposing from file

David Winsemius

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.
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Reading then transposing from file

Berend Hasselman
In reply to this post by diond

On 17-03-2012, at 10:27, 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.
>
> As you can imagine, I'd like to be able to use, say, foo$Var2 or foo$ID.
>
> What's the best way to achieve this?

I found this on Stackoverflow http://stackoverflow.com/questions/6778908/r-transposing-a-data-frame

and tried

ftext <- "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"

df.1 <- read.table(text=ftext, header=FALSE, stringsAsFactors=FALSE)
df.1
str(df.1)

df.names <- df.1[,1]
df.3 <- as.data.frame(t(df.1[,-1]), stringsAsFactors=FALSE)
colnames(df.3) <- df.names
rownames(df.3) <- NULL

# and maybe
df.3[,"Class"] <- as.numeric(df.3[,"Class"])
df.3

str(df.3) # Check the column types

Berend

______________________________________________
[hidden email] 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.