简体   繁体   中英

Replace space in in column title when looping through datasets in R

This is the code that I have:

for (file in file_list){
  temp_dataset <- read.csv(file) %>%
  select(Name, Age)

I have another variable called Eye Colour which has a space in it. When I add it to the select line the code fails.

I have a number of csv files that I'm trying to open and combine each on at the end of the one before it with only the three selected columns but one column has a space.

I need to change the space in the column title but its in over 100 csv files?

Try something like this. Notice the space in "Vendor Name". Just include it in single quotes in select() since that is how it shows in the head() output:

BCC2021Q3[,1:4] %>% head()

A tibble: 6 x 4

Title Directorate Vendor Name `Total Contract ~ 1 U0045 DISPOSAL OF WASTE ARISING IN B'HAM Neighbourhoods VEOLIA ES BIRMINGHA~ 87500000 2 S1 PPP SCHOOL REBUILDS Education & Skil~ BHAM SCHOOLS PARTNE~ 318000000 3 TMO BLOOMSBURY ESTATE MANAGEMENT BOARD Neighbourhoods BLOOMSBURY EMB 20000000 4 SBBCC_296 NORTHGATE HOUSING STOCK Digital & Custom~ NORTHGATE PUBLIC SE~ 3792616. 5 TMO HOLLY RISE HOUSING CO-OPERATIVE LTD Neighbourhoods HOLLYRISE HOUSING C~ 1132799. 6 TMO MANOR CLOSE RESIDENTS' MGT ORG LTD Neighbourhoods MANOR CLOSE RESIDEN~ 72890000

BCC2021Q3 %>% select('Vendor Name')

A tibble: 1,090 x 1

Vendor Name

1 VEOLIA ES BIRMINGHAM LTD
2 BHAM SCHOOLS PARTNERSHIP LTD
3 BLOOMSBURY EMB
4 NORTHGATE PUBLIC SERVICES UK LTD
5 HOLLYRISE HOUSING CO-OP LTD
6 MANOR CLOSE RESIDENTS MNGMT ORG LTD 7 POSTURITE LTD
8 NORTHGATE PUBLIC SERVICES UK LTD
9 GAILARDE LTD
10 SOPRA STERIA LTD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM