Home > Software engineering >  names attribute [3] must be the same length as the vector [2]
names attribute [3] must be the same length as the vector [2]

Time:01-28

Pls how can i rename each columns after str_split() operations in data frames

Here i created example data set

library(stringr)
#create data frame
samples <- paste0("sample", 1:5)
tags <- paste("tag", LETTERS[1:5],sep="_")
df1 <- data.frame(sample_plus_tag = paste(samples, tags, sep="_"))
#split sample columns
df1 <- df1 %>% mutate(sample=str_split_fixed(string=sample_plus_tag, pattern="_", n=2)) 
dim(df1)
names(df1) <- c("sample_plus_tag","sample","tags")

but i get the error. however i was thinking number of columns should be 3

[1] 5 2
Error in names(df1) <- c("sample_plus_tag", "sample", "tags") : 
  'names' attribute [3] must be the same length as the vector [2]

CodePudding user response:

Convert the matrix column to data.frame and then rename

df1 <- do.call(data.frame, df1)
names(df1) <- c("sample_plus_tag","sample","tags")

-output

> df1
  sample_plus_tag  sample  tags
1   sample1_tag_A sample1 tag_A
2   sample2_tag_B sample2 tag_B
3   sample3_tag_C sample3 tag_C
4   sample4_tag_D sample4 tag_D
5   sample5_tag_E sample5 tag_E

CodePudding user response:

What you could do is create the data frame as you did:

samples <- paste0("sample", 1:5)
tags <- paste("tag", LETTERS[1:5],sep="_")
df1 <- data.frame(sample_plus_tag = paste(samples, tags, sep="_"))

And then:

df1[c('sample', 'tags')] <- str_split_fixed(string = df1$sample_plus_tag, pattern = '_', n=2)

Output:

> df1
  sample_plus_tag  sample  tags
1   sample1_tag_A sample1 tag_A
2   sample2_tag_B sample2 tag_B
3   sample3_tag_C sample3 tag_C
4   sample4_tag_D sample4 tag_D
5   sample5_tag_E sample5 tag_E
  • Related