简体   繁体   中英

Separating string from multiple columns in R

I have a dataset that looks a bit like this

Species   Trend2001   TrendLimits2001   Trend 2002   TrendLimits2002
 Dog        -1.5        -1,-1.64          -1.0         -0.56,-2.0 
 Cat         2.3         2.0,3.1           1.75          1,2.2 
 Mouse       0.65       -0.3,1.0          1.02         -0.1,1.5

I want to split the "TrendLimits" columns into two columns called LowerLimits and UpperLimits. I know how to do this for each column separately but is there a way to get R to split all the columns based on the fact that they have "," present? In my actual dataset I have over 100 columns that need to be split and I don't wanna have to write the code out for each one.

What I hope my data looks like at the end is

Species   Trend2001    LowerLimits2001   UpperLimits2001  Trend 2002   LowerLimits2002    UpperLimits2002
 Dog        -1.5           -1                -1.64          -1.0         -0.56                -2.0 
 Cat         2.3           2.0                3.1           1.75          1                    2.2 
 Mouse       0.65         -0.3                1.0           1.02         -0.1                  1.5

I'm very new to R so please give detailed answers :) Thanks in advance!

We can use cSplit from splitstackshape

library(splitstackshape)
cSplit(df1, c("TrendLimits2001", "TrendLimits2002"), sep=",")

Or using base R , with read.csv , create the basename of columns ('v1') and paste the year value as suffix with paste0 and rep , then loop over the columns that have 'TrendLimits' as substring in column names, read with read.csv bind the list of data.frames with cbind and update the original dataset by assignment

v1 <- c("LowerLimits", "UpperLimits")
nm1 <- paste0(v1, rep(2001:2002, each = length(v1)))
nm2 <- grep("TrendLimits", names(df1), value = TRUE)
df1[nm1] <- do.call(cbind, lapply(df1[nm2], function(x)
     read.csv(text = as.character(x), header = FALSE)))
df1[nm2] <- NULL
df1
#  Species Trend2001 Trend2002 LowerLimits2001 UpperLimits2001 LowerLimits2002 UpperLimits2002
#1     Dog     -1.50     -1.00            -1.0           -1.64           -0.56            -2.0
#2     Cat      2.30      1.75             2.0            3.10            1.00             2.2
#3   Mouse      0.65      1.02            -0.3            1.00           -0.10             1.5

data

df1 <- structure(list(Species = c("Dog", "Cat", "Mouse"), Trend2001 = c(-1.5, 
2.3, 0.65), TrendLimits2001 = c("-1,-1.64", "2.0,3.1", "-0.3,1.0"
), Trend2002 = c(-1, 1.75, 1.02), TrendLimits2002 = c("-0.56,-2.0", 
"1,2.2", "-0.1,1.5")), class = "data.frame", row.names = c(NA, 
-3L))

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