Johnny Strings - 11 months ago 52

R Question

In this given R code...

`df1 <- c("a","b","a","b","b","c","b","c","c","d","c","d")`

dim(df1) = c(4,3)

df1 <- as.data.frame(t(df1))

for (i in 1:nrow(df1)) {

df1[i,"paste"] <- paste(df1[i,!names(df1) %in% c("paste")],collapse="")

}

... I am expecting to see:

`V1 V2 V3 V4 paste`

a b a b abab

b c b c bcbc

c d c d cdcd

... but am instead seeing:

`V1 V2 V3 V4 paste`

a b a b 1111

b c b c 2222

c d c d 3333

This seems trivial, and I do not understand what's wrong with code. Could anyone explain how my code is producing the given output? Also, any other ideas on how to produce the simple output I desire? Thanks.

Answer Source

The reason is that `df1[i,!names(df1) %in% c("paste")]`

returns a data frame. You can unlist the data frame in order for `paste`

to work properly:

```
for (i in 1:nrow(df1)) {
df1[i,"paste"] <- paste(unlist(df1[i,!names(df1) %in% c("paste")]),collapse="")
# ^^^^^^
}
df1
# V1 V2 V3 V4 paste
#1 a b a b abab
#2 b c b c bcbc
#3 c d c d cdcd
```

Here is how `paste`

works on data frame if `df1`

contains factor variable:

```
paste(df1[1:4], collapse = "")
# [1] "1:31:31:31:3"
```

As an alternative, this should also work for you:

```
df1$paste = do.call(paste0, df1[1:4])
```