Megatron
Megatron

Reputation: 17099

Extra column using tidyr's `unite_` vs `unite`

In the following example, why is there an additional column in the unite_() output vs the unite() output?

library(tidyr)

x1 <- data.frame(Sample=c("A", "B"), "1"=c("-", "y"), 
                 "2"=c("-", "z"), "3"=c("x", "a"), check.names=F)
#   Sample 1 2 3
# 1      A - - x
# 2      B y z a

Here we see the desired output:

unite(x1, mix, 2:ncol(x1), sep=",")
#   Sample   mix
# 1      A -,-,x
# 2      B y,z,a

Why is there an additional column here (the 1 column)? The default is to remove the columns used by unite_().

unite_(x1, "mix", 2:ncol(x1), sep=",")
#   Sample 1   mix
# 1      A - -,-,x
# 2      B y y,z,a

Note: tidyr version 0.5.1

Upvotes: 1

Views: 1094

Answers (2)

Motoman
Motoman

Reputation: 11

I tried with "Unite" but it did not work. However, It worked very well with "paste" function.

df$new_col <- paste(df$col1,df$col2,sep="-") or if you have more columns to join,

df$new_col <- paste(df$col1,df$col2,df$col3,....,sep="-")

Upvotes: 0

Dave2e
Dave2e

Reputation: 24129

The syntax are slightly different between the two usages:

#unite(data, col, ..., sep = "_", remove = TRUE)
#unite_(data, col, from, sep = "_", remove = TRUE)

From the unite_ help page, the from option is defined as: "Names of existing columns as character vector."

Use column names as opposed to the column numbers provided the desired results:

unite_(x1, "mix", names(x1[,2:ncol(x1)]), sep=",")
#  Sample   mix
#1      A -,-,x
#2      B y,z,a

Upvotes: 2

Related Questions