Martin
Martin

Reputation: 1

How to get gsub output as dataframe

My goal is to replace "--" with "to" in the following data frame

df:
                region      30-44 (95% CI)    45-54 (95% CI)    55-64 (95% CI)     65-74 (95% CI)      Total (95% CI)
1               Africa 10.25 (3.28--17.23) 2.82 (0.93--4.71) 2.16 (0.95--3.36) 1.23 (-1.48--3.93) 16.46 (8.66--24.25)
2                 Asia  8.27 (3.29--13.25) 2.80 (0.86--4.74) 2.39 (0.94--3.85) 1.63 (-2.20--5.45) 15.09 (8.48--21.70)
3               Europe  8.36 (3.50--13.21) 3.34 (1.10--5.59) 3.17 (1.42--4.91) 2.30 (-2.79--7.39) 17.17 (9.60--24.73)
4 L.A. and the Caribb. 10.48 (3.41--17.56) 3.05 (0.91--5.19) 2.57 (1.05--4.09) 1.87 (-2.37--6.12) 17.97 (9.39--26.56)
5     Northern America  7.28 (2.31--12.26) 2.86 (0.65--5.07) 2.91 (0.97--4.84) 2.35 (-3.16--7.86) 15.39 (7.69--23.09)
6              Oceania  7.01 (2.20--11.82) 2.72 (0.61--4.83) 2.78 (0.91--4.66) 2.30 (-3.09--7.68) 14.81 (7.32--22.30)
7               Global  8.44 (3.36--13.52) 2.92 (0.96--4.88) 2.59 (1.15--4.03) 1.82 (-2.31--5.96) 15.77 (8.80--22.74)

I'm applying gsub as follows: data_to <- gsub("--", " to ", df)

data_to:

[1] "c(\"Africa\", \"Asia\", \"Europe\", \"L.A. and the Caribb.\", \"Northern America\", \"Oceania\", \"Global\")"                                                                                  
[2] "c(\"10.25 (3.28 to 17.23)\", \"8.27 (3.29 to 13.25)\", \"8.36 (3.50 to 13.21)\", \"10.48 (3.41 to 17.56)\", \"7.28 (2.31 to 12.26)\", \"7.01 (2.20 to 11.82)\", \"8.44 (3.36 to 13.52)\")"     
[3] "c(\"2.82 (0.93 to 4.71)\", \"2.80 (0.86 to 4.74)\", \"3.34 (1.10 to 5.59)\", \"3.05 (0.91 to 5.19)\", \"2.86 (0.65 to 5.07)\", \"2.72 (0.61 to 4.83)\", \"2.92 (0.96 to 4.88)\")"              
[4] "c(\"2.16 (0.95 to 3.36)\", \"2.39 (0.94 to 3.85)\", \"3.17 (1.42 to 4.91)\", \"2.57 (1.05 to 4.09)\", \"2.91 (0.97 to 4.84)\", \"2.78 (0.91 to 4.66)\", \"2.59 (1.15 to 4.03)\")"              
[5] "c(\"1.23 (-1.48 to 3.93)\", \"1.63 (-2.20 to 5.45)\", \"2.30 (-2.79 to 7.39)\", \"1.87 (-2.37 to 6.12)\", \"2.35 (-3.16 to 7.86)\", \"2.30 (-3.09 to 7.68)\", \"1.82 (-2.31 to 5.96)\")"       
[6] "c(\"16.46 (8.66 to 24.25)\", \"15.09 (8.48 to 21.70)\", \"17.17 (9.60 to 24.73)\", \"17.97 (9.39 to 26.56)\", \"15.39 (7.69 to 23.09)\", \"14.81 (7.32 to 22.30)\", \"15.77 (8.80 to 22.74)\")"

The problem is that the output is in character format whereas I want it to be in data frame format. Does anyone know how to have the output be a data frame? Any assistance appreciated, thanks.

Upvotes: 0

Views: 162

Answers (1)

akrun
akrun

Reputation: 887571

gsub expects a vector or matrix as input. According to ?gsub

x, test - a character vector where matches are sought, or an object which can be coerced by as.character to a character vector. Long vectors are supported.

We could loop over the columns of data.frame (other than the first column - region) and apply gsub as a column is a vector

df[-1] <- lapply(df[-1], gsub, pattern = "--", replacement = " to ")

Upvotes: 1

Related Questions