Question: problems in data frame
0
gravatar for niutster
2.1 years ago by
niutster80
niutster80 wrote:

Dear all,

I have a two text files contain gene names and methylation degree. I have read these files with scan command in R and named them data1 and data2. Gene names and methylation degree have character and numeric type, respectively . when i combine them in one data frame with below command, it does not work correctly. All of them changes to a data frame with string type with number view and does not preserve their types.

df=data.frame(data1, data2)
R scan command data frame • 689 views
ADD COMMENTlink modified 2.0 years ago by Santosh Anand4.9k • written 2.1 years ago by niutster80
2

There's rarely a reason to use scan for such things, that's what read.delim() and such are for.

Please post a couple lines from data1 and data2 (or better yet, the original files before you read them in).

ADD REPLYlink written 2.1 years ago by Devon Ryan91k

1.) This is not a bioinformatics question.

2.) read the helps:

?I
?data.frame
?as.data.frame
ADD REPLYlink written 2.1 years ago by h.mon26k
0
gravatar for Santosh Anand
2.0 years ago by
Santosh Anand4.9k
Santosh Anand4.9k wrote:

Try this

# install package data.table
install.packages("data.table")
data1 = as.data.frame(fread("gene_names.txt"))
data2 = as.data.frame(fread("methylation.txt"))
df = data.frame(data1, data2, stringsAsFactors = F)
ADD COMMENTlink written 2.0 years ago by Santosh Anand4.9k
1

You forgot to library(data.table). BTW, why do you need as.data.frame on data.table object (fread output)? Why not to:

data.table(gene = fread("gene.txt"), 
           meth = fread("meth.txt"))
ADD REPLYlink modified 2.0 years ago • written 2.0 years ago by PoGibas4.8k

Yes, that's a much better way. In my mind, I was thinking to be as close to the OPs request as possible (so getting data1 and data2 as individual data.frames).

data.table can be quirky sometimes (e.g. column manipulation), although the class is both data.table and data.frame. Also, the printing of data.table is different.

ADD REPLYlink written 2.0 years ago by Santosh Anand4.9k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 702 users visited in the last hour