R tidyverse way to change cell content (string) based on a keyword condition with content of another cell plus text

帅比萌擦擦* 提交于 2019-12-11 18:26:41

问题


I'm just starting to learn tidyverse approaches and currently I am looking how to solve the following problem:

in the following dataframe:

i'm looking to replace all the "sensitivity level" cells with the name of the channel found 1 row up, in the 2nd column, combined with the word "sensitivity"

after successfully doing so I want to run it through the other tidyverse solution to the other part of my problem that is posted here

this is the code to generate an exact replica of my dataframe:

df <- structure(list(Parameter = c("Trigger level (mV)", "Smart Triggered!", 
"FL Red Maximum > 9", "CytoUSB Block size", "Instrument", "Beam width", 
"Core speed", "User Comments", "Measurement date", "Measurement duration", 
"Flow rate (µL/sec)", "Channel 1", "Channel 2", "Channel 3", 
"Channel 4", "  sensitivity level", "Channel 5", "  sensitivity level", 
"Channel 6", "  sensitivity level", "Channel 7", "  sensitivity level", 
"Total number of particles", "Smart triggered number of particles", 
"Concentration (part/µL)", "Volume (µL)"), Value = c(" 10.49", 
"", "", " Auto (maxTimeOut 5s )", " EMSO II", " 5", " 2.2", " ", 
" 15-Apr-16 5:12:21 AM", " 36", " 2.06", " Trigger1", " FWS L", 
" FWS R", " SWS  TRIGGER", " 55", " FL Yellow", " 100", " FL Orange", 
" 100", " FL Red", " 100", " 1344", " 1344", " 2062.71614038604", 
" 46.7626146474752")), class = "data.frame", row.names = c(NA, 
-26L))

回答1:


This should give what you want:

library(dplyr)

df %>% 
  mutate(Parameter = ifelse(
    Parameter == "  sensitivity level",
    paste(lag(Value), "sensitivity"),
    Parameter
  ))


来源:https://stackoverflow.com/questions/55346736/r-tidyverse-way-to-change-cell-content-string-based-on-a-keyword-condition-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!