Search code examples
rfor-looptrim

R for loop help to trim based on condition


I am new to R and struggling with for loop: I want to split some strings in a df based on condition my df: MyDF

I want to split where begins with "X" to identify I am using - grepl("X.",df1[,1]) to split - str_split_fixed(df1[,1],"X",2)[,2] and not sure how to incorporate that in the loop...

for (i in df1[,1]){
  # if (begins with X) then split
}

so the goal here is to strip "X" from df rows (11 & 12)

Thank you in advance!


Solution

  • R is a vectorized language, so you can just substitute the leading "X" with "" in one line.

    df1[,1] <- sub("^X", "", df1[,1])
    

    Using for loop would be very inefficient in this case, but if you insist on that, then

    for (i in seq_along(df1[,1])) {
      if (substr(df1[i,1],1,1) == "X")
        df1[i,1] <- substring(df1[i,1],2)
    }
    

    Data

    df1 <- structure(list(header1 = c("PLAYERID", "YEARID", "STINT", "TEAMID", 
    "LGID", "G", "G_BATTING", "AB", "R", "H", "X2B", "X3B", "HR", 
    "RBI", "SB", "CS", "BB", "SO", "IBB", "HBP", "SH", "SF", "GIDP", 
    "G_OLD")), class = "data.frame", row.names = c(NA, -24L))