Learner
Learner

Reputation: 757

how can I merge a data into one?

I have a data looks like this

1.1.1.1     Alcohol dehydrogenase.
1.1.1.2     Alcohol dehydrogenase (NADP(+)).
1.1.1.3     Homoserine dehydrogenase.
1.1.1.4     (R,R)-butanediol dehydrogenase.
1.1.1.5     Transferred entry: 1.1.1.303 and 1.1.1.304.
1.1.1.6     Glycerol dehydrogenase.
1.1.1.7     Propanediol-phosphate dehydrogenase.
1.1.1.8     Glycerol-3-phosphate dehydrogenase (NAD(+)).
1.1.1.9     D-xylulose reductase.
1.1.1.10    L-xylulose reductase.

I load it with read.table like this

df <- read.table("path to data", header=F, fill=T)

and I get the following data

df <- structure(list(V1 = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 2L), .Label = c("1.1.1.1", "1.1.1.10", "1.1.1.2", "1.1.1.3", 
"1.1.1.4", "1.1.1.5", "1.1.1.6", "1.1.1.7", "1.1.1.8", "1.1.1.9"
), class = "factor"), V2 = structure(c(2L, 2L, 6L, 1L, 9L, 4L, 
8L, 5L, 3L, 7L), .Label = c("(R,R)-butanediol dehydrogenase.", 
"Alcohol", "D-xylulose", "Glycerol", "Glycerol-3-phosphate", 
"Homoserine", "L-xylulose", "Propanediol-phosphate", "Transferred"
), class = "factor"), V3 = structure(c(3L, 2L, 3L, 1L, 4L, 3L, 
3L, 2L, 5L, 5L), .Label = c("", "dehydrogenase", "dehydrogenase.", 
"entry:", "reductase."), class = "factor"), V4 = structure(c(1L, 
3L, 1L, 1L, 4L, 1L, 1L, 2L, 1L, 1L), .Label = c("", "(NAD(+)).", 
"(NADP(+)).", "1.1.1.303"), class = "factor"), V5 = structure(c(1L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "and"), class = "factor"), 
    V6 = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("", 
    "1.1.1.304."), class = "factor")), class = "data.frame", row.names = c(NA, 
-10L))

I use fill=T because if I don't then it gives me error

df <- read.table("path/example.txt", header=F, fill=F)
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  : 
  line 1 did not have 6 elements

Is there a way that I load the data and have two columns? or bring the data together so that I will have two column in R?

Note that I can do that with read.delim but it will make problem with another code that I am using

my desire output is like

Upvotes: 0

Views: 33

Answers (2)

Jonny Phelps
Jonny Phelps

Reputation: 2727

Not pretty, but here is a possible solution:

# seperator is multi-space, but not possible in R
file_data <- "1.1.1.1     Alcohol dehydrogenase.
1.1.1.2     Alcohol dehydrogenase (NADP(+)).
1.1.1.3     Homoserine dehydrogenase.
1.1.1.4     (R,R)-butanediol dehydrogenase.
1.1.1.5     Transferred entry: 1.1.1.303 and 1.1.1.304.
1.1.1.6     Glycerol dehydrogenase.
1.1.1.7     Propanediol-phosphate dehydrogenase.
1.1.1.8     Glycerol-3-phosphate dehydrogenase (NAD(+)).
1.1.1.9     D-xylulose reductase.
1.1.1.10    L-xylulose reductase."

# change sep from 4 spaces to \t, which is identifiable. 
# replace textConnection(file_data) with your data file name
read_text <- readLines(textConnection(file_data ))
altered_text <- gsub("    ", "\t", read_text)

# parsing from altered text
df <- read.delim(textConnection(altered_text), header=FALSE, sep="\t", fill=TRUE)
df

The issue is your separator is more than one character (http://r.789695.n4.nabble.com/multiple-separators-in-sep-argument-for-read-table-td856567.html).

Alternatives would be to change the date pre-loading to have a common separator between columns. Otherwise read data as you have already, and add a data step to concatenate the columns 2 onwards in to 1 column e.g. using paste.

Upvotes: 1

JdeMello
JdeMello

Reputation: 1718

Using base R, you can use Reduce() with paste() and then trim the white spaces with trimws() to produce another data.frame:

df2 <- data.frame(V1 = df[1], V2 = trimws(Reduce(paste, df[-1])))

> df2
         V1                                           V2
1   1.1.1.1                       Alcohol dehydrogenase.
2   1.1.1.2             Alcohol dehydrogenase (NADP(+)).
3   1.1.1.3                    Homoserine dehydrogenase.
4   1.1.1.4              (R,R)-butanediol dehydrogenase.
5   1.1.1.5  Transferred entry: 1.1.1.303 and 1.1.1.304.
6   1.1.1.6                      Glycerol dehydrogenase.
7   1.1.1.7         Propanediol-phosphate dehydrogenase.
8   1.1.1.8 Glycerol-3-phosphate dehydrogenase (NAD(+)).
9   1.1.1.9                        D-xylulose reductase.
10 1.1.1.10                        L-xylulose reductase.

Upvotes: 0

Related Questions