Home > Blockchain >  Change multiple columns to as.character at once
Change multiple columns to as.character at once

Time:10-28

I want to use pivot longer on my df but not all of my columns are as.characters, as i have 10 columns i need to change does anyone know how to do this in one argument to save me writing it out for all of the years?

My df looks like this;

fisheries_df
`Series Name`  `Country Name` `1997` `1998` `1999` `2000` `2001` `2002` `2003` `2004` `2005` `2006` `2007` `2008` `2009` `2010` `2011` `2012`
 1 Total fisheri… Albania        1110.8 2807.5 3057.9 3635   3597.2 4516.8 4274.6 6118.5 6473   7.71e3 7.51e3 7.36e3 8.13e3 7.85e3 7.35e3 1.23e4
 2 Total fisheri… Algeria        91907… 92620  102649 11351… 134082 13480… 141376 11405… 12662… 1.46e5 1.48e5 1.42e5 1.30e5 9.52e4 1.04e5 1.08e5
 3 Total fisheri… Cyprus         25788  20482  41060  70223  56606  142941 49561  82269  64933  3.35e4 5.65e3 5.40e3 5.11e3 5.55e3 5.85e3 5.66e3

the code and error message im using to pivot the df is below, assuming the character issue is my problem here;

fisheries_longer = pivot_longer(fisheries_df, c(3:26), c('year')) 

Error in `stop_vctrs()`:
! Can't combine `1997` <character> and `2006` <double>.
Run `rlang::last_error()` to see where the error occurred.

CodePudding user response:

Try this . It is using @akruns's values_transform() but with list: See here: pivot_longer: values_ptypes: can't convert <integer> to <character>

library(dplyr)
library(tidyr)
df %>% 
  pivot_longer(
    cols = -c(1:3),
    names_to = "key",
    values_to = "val", 
    values_transform = list(val = as.character))
   X.Series Name. X.Country key     val    
      <int> <chr> <chr>     <chr>   <chr>  
 1        1 Total fisheri   Name..1 Albania
 2        1 Total fisheri   X.1997. 1110.8 
 3        1 Total fisheri   X.1998. 2807.5 
 4        1 Total fisheri   X.1999. 3057.9 
 5        1 Total fisheri   X.2000. 3635   
 6        1 Total fisheri   X.2001. 3597.2 
 7        1 Total fisheri   X.2002. 4516.8 
 8        1 Total fisheri   X.2003. 4274.6 
 9        1 Total fisheri   X.2004. 6118.5 
10        1 Total fisheri   X.2005. 6473   
# ... with 24 more rows
# i Use `print(n = ...)` to see more rows
df <- structure(list(X.Series = 1:2, Name. = c("Total", "Total"), X.Country = c("fisheri", 
"fisheri"), Name..1 = c("Albania", "Algeria"), X.1997. = c(1110.8, 
91907), X.1998. = c(2807.5, 92620), X.1999. = c(3057.9, 102649
), X.2000. = c(3635L, 11351L), X.2001. = c(3597.2, 134082), X.2002. = c(4516.8, 
13480), X.2003. = c(4274.6, 141376), X.2004. = c(6118.5, 11405
), X.2005. = c(6473L, 12662L), X.2006. = c(7710, 146000), X.2007. = c(7510, 
148000), X.2008. = c(7360, 142000), X.2009. = c(8130, 130000), 
    X.2010. = c(7850, 95200), X.2011. = c(7350, 104000), X.2012. = c(12300, 
    108000)), class = "data.frame", row.names = c(NA, -2L))

CodePudding user response:

It is possible that a single column/multiple columns are character class. We could use values_transform to convert to character type (as character and then modify if needed

library(tidyr)
pivot_longer(fisheries_df, c(3:26), 'year', values_transform = as.character) 

Or change it to as.numeric - if there are non-numeric elements, it will get converted to NA

pivot_longer(fisheries_df, c(3:26), 'year', values_transform = as.numeric)
  •  Tags:  
  • r
  • Related