Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
次のようなデータフレームがあります。
df <- data.frame(one=c("s1_below_10", "s2_below_20"), two=c("s3_above_10","s4_above_10"))
すべての文字列を最初のアンダースコアの前の数字に置き換えたいです。言い換えれば、望ましい出力は
1 3 2 4
この置換を実行する方法を知りたいです (データセットが非常に大きい)。ご協力いただきありがとうございます。
基本的なgsub呼び出しは次のようになります。
gsub
gsub("^.+?(\\d+)_.+","\\1",df$one) [1] "1" "2"
lapply各列にできること:
lapply
data.frame(lapply(df, gsub, pattern="^.+(\\d+)_.+",replacement= "\\1")) one two 1 1 3 2 2 4