Problem with mutate when trying to create a line_id column

54 Views Asked by At

I need to create a line ID column within a dataframe for further pre-processing steps. The code worked fine up until yesterday. Today, however I am facing the error message: "Error in mutate(): ℹ In argument: line_id = (function (x, y) .... Caused by error: ! Can't convert y to match type of x ."

Here is my code - the dataframe consists of two character columns:

split_text <- raw_text %>%
  mutate(text = enframe(strsplit(text, split = "\n", ))) %>%
  unnest(cols = c(text)) %>%
  unnest(cols = c(value)) %>%
  rename(text_raw = value) %>%
  select(-name) %>%
  mutate(doc_id = str_remove(doc_id, ".txt")) %>% 
  # removing empty rows + add line_id
  mutate(line_id = row_number())

Besides row_number(), I also tried rowid_to_column, and even c(1:1000) - the length of the dataframe. The error message stays the same.

2

There are 2 best solutions below

3
Talha Asif On

Try explicitly specifying the data type of the "line_id" column as an integer using the as.integer() function, like this:

mutate(line_id = as.integer(row_number()))
0
stacksterppr On

This code works but is not fully satisfying, since I have to break the pipe:

split_text$line_id <- as.integer(c(1:nrow(split_text)))