[英]R data table : How to find unknown number of empty cells directly below a specific cell and fill them with numbered strings
I know how to find empty cells
in a data.table
in general, but this is a little trickier and I haven't quite figured out how to manage this. 我知道如何在
data.table
中找到空cells
,但这有点棘手,我还没弄清楚如何管理它。
Lets say I have a data.table
which in df[2,1]
contains my keyword string i need to target: "Smart triggered!" 假设我有一个
data.table
,在df[2,1]
包含我需要定位的关键字字符串:“智能触发!” in this case. 在这种情况下。
The amount of smart triggers used by the user of the instrument can vary, but they result in 1 empty cell below df[2,1]
for each trigger, and their name in the 2nd column
仪器用户使用的智能触发器的数量可能会有所不同,但是对于每个触发器,它们会导致低于
df[2,1]
1个空单元格,以及在第2 column
名称
I'm looking for a way to figure out how many empty cells
are directly below "Smart triggered!" 我正在寻找一种方法来确定“智能触发!”正下方有多少个空
cells
。 and fill them sequentially with "Smart trigger 1", Smart trigger 2"... until we hit the first next cell
in column
1 that contains something ('Instrument'). There are potential other empty cells further down in the table that I do not want to alter. 然后用“智能触发器1”,智能触发器2“顺序填充它们......直到我们点击第1
column
中包含某些内容的第一个下一个cell
(”仪器“)。表格中还有其他可能的空单元格不想改变。
In this case my info files read into a data.table
looking like this: 在这种情况下,我的信息文件读入
data.table
如下所示:
df <- data.frame(name = c("Trigger", "Smart Triggered!", "", "", "Instrument", "Beam", "Core speed", "Channel1", "Channel2", "Channel3", "Channel4", ""),
values = c("SWS", "", "FLRED", "FLORANGE", "Demo", "5um", "2.2", "FWS", "SWS", "FLRED", "FLORANGE", "x"))
I suspect perhaps a while loop, but there are probably better data table solutions than that. 我怀疑可能是一个while循环,但可能有更好的数据表解决方案。
see if this works for you: 看看这是否适合你:
library(tidyverse)
df <- data.frame(name = c("Trigger", "Smart Triggered!", "", "", "Instrument", "Beam", "Core speed", "Channel1", "Channel2", "Channel3", "Channel4", ""),
values = c("SWS", "", "FLRED", "FLORANGE", "Demo", "5um", "2.2", "FWS", "SWS", "FLRED", "FLORANGE", "x"))
df %>%
mutate(new_name = ifelse(as.character(name) == "", NA, as.character(name))) %>%
tidyr::fill(new_name) %>%
split(.$new_name) %>%
map_df(., ~.x %>% mutate(row_no = row_number(),
row_no = lag(row_no),
new_name1 = ifelse(is.na(row_no),
as.character(new_name),
paste0(as.character(new_name), "_", row_no)))) %>%
select(name, new_name, new_name1, values) %>%
full_join(df, .) %>%
mutate(name = as.character(name)) %>%
mutate(name = ifelse(new_name == "Smart Triggered!", new_name1, name)) %>%
select(name, values)
You can use a lapply
function. 您可以使用
lapply
函数。 I dont know how is your for loop
solution, but mine requiere using <<-
which is not ideal so maybe you prefer to stick to a for loop
我不知道你的
for loop
解决方案是怎么回事,但是我使用<<-
这是不理想的,所以也许你更喜欢坚持使用for loop
library(data.table)
df <- data.frame(name = c("Trigger", "Smart Triggered!", "", "", "Instrument", "Beam", "Core speed", "Channel1", "Channel2", "Channel3", "Channel4", ""),
values = c("SWS", "", "FLRED", "FLORANGE", "Demo", "5um", "2.2", "FWS", "SWS", "FLRED", "FLORANGE", "x"))
df <- as.data.table(df)
df$name <- as.character(df$name)
counting <<- 1
df$name[2:nrow(df)] <- unlist(lapply(2:nrow(df), function(x){
if((df[x,]$name=="") && (df[x-counting,]$name=="Smart Triggered!")){
counting <<- counting + 1
return(paste0("Smart trigger ", counting))
}
else{
counting <<- 1
return(df[x,]$name)
}
}
))
df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.