Skip to content

How to remove space among columns

8 messages · Manuel Spínola, Yu-Chun Kao, Pierre THIRIET +2 more

#
Hi Manuel,
You can use the function "paste"

paste(col1,col2.col3,col4, sep = "")

in a new data frame.

data.frame(
col1 = paste(col1,col2.col3,col4, sep = ""),
col2 = col5,
col3 = col6)

I think it will work
Yu-Chun


2013/3/27 Manuel Sp?nola <mspinola10 at gmail.com>:
#
Manuel, you've been provided by a few alternatives. Here's mine.

x <- read.table(text ="col1  col2 col3 col4 col5 col6
 0        1      1     0     a    c
 1        0      0     0     a    d
 0        1      1     1     b    d", header = TRUE)

x$newx <- apply(x[, 1:4], 1, paste, collapse = "")
col1 col2 col3 col4 col5 col6 newx
1    0    1    1    0    a    c 0110
2    1    0    0    0    a    d 1000
3    0    1    1    1    b    d 0111

I opted for a new column, but you can construct your data.frame anyway you want.

Cheers,
Roman


On Wed, Mar 27, 2013 at 2:32 PM, Pierre THIRIET
<pierre.d.thiriet at gmail.com> wrote:
--
In God we trust, all others bring data.
#
l=data.frame(col1=c(0,1,0), col2=c(1,0,1), col3=c(1, 0, 1), col4=c(0, 0, 
1), col5=c("a", "a", "b"), col6=c("c", "d", "d"))
ll=paste(l$col1, l$col2, l$col3, l$col4, sep="")
data.frame(ll, a=l$col5, b=l$col6)

see ?paste

bret
On 3/27/2013 8:15 AM, Manuel Sp?nola wrote: