Home > other >  Filling/replacing an empty cell in a column with the first character in a string from a previous col
Filling/replacing an empty cell in a column with the first character in a string from a previous col

Time:06-26

I have a column of data labeled in the following string format: "1.2.a.2021"; I want to replace the corresponding row in a "wk#" column currently filled with NA to the first number in the string (in this case it would be "1"). I am using strsplit() to split up the string into various characters/or made them numeric, but I want to create a for loop that will then take that first character from each row/first column and put it into the new column respective cell. There are 13 weeks in the but hundreds of data points so a loop would work best to find and match the number.
See example below.

sample ng wk#
1.2.a.2021 0.4 NA

CodePudding user response:

We may use sub to match the . followed by other characters and replace with blank

df1[["wk#"]] <- sub("\\..*", "", df1$sample)

Or may use substr as well if the intention is to get the first character

df1[["wk#"]] <- substr(df1$sample, 1, 1)

When we use strsplit to split at the ., it needs a loop to extract the first element from the list.

df1[["wk#"]] <- sapply(strsplit(df1$sample, ".", fixed = TRUE), `[`, 1)

CodePudding user response:

@akrun already provides nearly all possible solution. Here is one more making use of sub and a different regular expression:

df$wk <- sub('(^.).*', '\\1', df$sample)

      sample  ng wk
1 1.2.a.2021 0.4  1
  • Related