如何将给定的文本填写到另一个给定文本,等等

问题描述 投票:0回答:1
很可能已经回答了,但是我正在努力找到这个问题的答案: 在新的列“ new_text”中,如何将给定文本填充到另一个给定文本,依此类推...

在下面的示例中,如何将“波特”填充到“ wisley”,然后“ wisley”对“ Granger”等...?

该想法是通过选择以这种方式填充的一系列特定单词来将提出的解决方案应用于数千行(使用pdftools :: pdf_data获得)的数据框。 thanks寻求帮助

> dat0 text new_text 1 Potter Potter 2 hj7d Potter 3 kl8ep Potter 4 f3d Potter 5 rtyzs2 Potter 6 Wisley Wisley 7 lq6s Wisley 8 2fg Wisley 9 Granger Granger 10 r8ka Granger 11 h9 Granger 12 qm9ne Granger

data:

dat0 <- structure(list(text = c("Potter", "hj7d", "kl8ep", "f3d", "rtyzs2", "Wisley", "lq6s", "2fg", "Granger", "r8ka", "h9", "qm9ne"), new_text = c("Potter", "Potter", "Potter", "Potter", "Potter", "Wisley", "Wisley", "Wisley", "Granger", "Granger", "Granger", "Granger")), class = "data.frame", row.names = c(NA, -12L))

	

一种方法是将非名称转换为na,然后从

fill
使用
tidyr
r dplyr text fill mutate
1个回答
0
投票
library(tidyr) Names <- c("Potter", "Wisley", "Granger") Non_names <- setdiff(dat0$text, Names) transform(dat0, text=ifelse(text %in% Non_names, NA, text)) |> fill(text) text new_text 1 Potter Potter 2 Potter Potter 3 Potter Potter 4 Potter Potter 5 Potter Potter 6 Wisley Wisley 7 Wisley Wisley 8 Wisley Wisley 9 Granger Granger 10 Granger Granger 11 Granger Granger 12 Granger Granger

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.