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

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!
CodePudding user response:
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))
CodePudding user response:
for loop
for(i in seq_along(df$header1)){ df$header1[i] <- sub("^X","",df$header1[i]) }
df
header1
1 DGIGACAE
2 DFGGFAEHBD
3 BIBH
4 EB
5 DHBDFC
6 2BD
7 3GDE
8 DEAEFGE
9 I
10 FFBGDBD
Better vectorized
df$header1 <- sub("^X","",df$header1)
df
header1
1 DGIGACAE
2 DFGGFAEHBD
3 BIBH
4 EB
5 DHBDFC
6 2BD
7 3GDE
8 DEAEFGE
9 I
10 FFBGDBD
Data
df <- structure(list(header1 = c("DGIGACAE", "DFGGFAEHBD", "BIBH",
"EB", "DHBDFC", "X2BD", "X3GDE", "DEAEFGE", "I", "FFBGDBD")), row.names = c(NA,
-10L), class = "data.frame")
