Marcos Guimaraes
Marcos Guimaraes

Reputation: 1295

Concatenate two columns from table in R

I have a table stored in a csv file that looks like this:

"","",""
"1",50.7109704392639,598.945216481663
"2",88.4551431247316,432.427671968179
"3",146.142850442859,558.077250358249
"4",67.5287612139969,283.50009457641
"5",28.8212787088875,355.3292769956

I am trying to concatenate the second and the third columns from this table into an array by doing this:

data <- read.table("testecase3.csv", header = TRUE, sep = ",")

before <- data[2];
after <- data[3];

merge <- c(before, after);

When I print this new array, this is what I get:

$`X.1`
 [1]  50.71097  88.45514 146.14285  67.52876  28.82128 


$X.2
 [1]  598.9452  432.4277  558.0773  283.5001  355.3293

How can I fix this problem? I would like something like this:

[1]  50.71097  88.45514 146.14285  67.52876  28.82128  598.9452  432.4277  558.0773  283.5001  355.3293

Upvotes: 0

Views: 470

Answers (1)

Marcos Guimaraes
Marcos Guimaraes

Reputation: 1295

The correct way do do this is using:

before <- data[,2]; after <- data[,3];

As Darren explained above, data[2] extracts the entire column 2 as a data.frame, whereas data[ , 2] extracts the elements in the column 2 as a vector.

Upvotes: 1

Related Questions