R tidyverse way to change cell content (string) ba

2019-08-20 06:38发布

问题:

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
  ))