Creating a new column based on another column

Hi,

What I'm attempting is probably rather simple but I have no idea how to google it or where to start. I have a data frame (see below) of acoustic data. All I want to do is create a new column that states which week number the row belongs to. The first column in the data frame (Process_ID) is basically that column, all I want to do is create a new column where the Process_ID number corresponds with just "1" or "2" and so on..

For example, the first week of my data set can be identified using the Process_ID number "74250". I just want to make a corresponding column that will list "1" on rows that have "74250" in the Process_ID column.

Process_ID Interval Layer Height_mean Layer_depth_min Layer_depth_max Ping_
<
1 74250 1 1 11.8 3.14 103. 0 865 432 0 0 0
2 74250 2 1 11.9 3.14 103. 866 1730 1298 0 0 0
3 74250 3 1 11.9 3.14 103. 1731 2595 2163 0 0 0
4 74250 4 1 12.0 3.14 103. 2596 3460 3028 0 0 0
5 74250 5 1 12.0 3.14 103. 3461 4325 3893 0 0 0
6 74250 6 1 11.9 3.14 103. 4326 5190 4758 0 0 0

This should work:

library(dplyr)

Process_ID %>% 
  mutate(Process_ID_number = if_else(Process_ID == 74250, 1, 0))

Next time, provide a reproducible example:

Agreed about the reprex. How about:

1 Like

I'd provide a reproducible example but I have no idea where to even start on this one. All I can do is give a data set.

also, "if_else" is a no go, R doesn't seem to like it and I need this to rename up to 9 numbers consecutively.

Hi
Hope something like this helps

library(shiny)
library(shinydashboard)

ui <- fluidPage(
box(
width = 4,height = "350px",align='center',
HTML(paste('

',"1st Week Process ID",'
')),
splitLayout(cellWidths = c("25%","25%"),
numericInput(inputId = "week1startid",label = "Starting ID",value = 0,width = "100px"),
numericInput(inputId = "week1endid",label = "Finishing ID",value = 0,width = "100px")
),
HTML(paste('
',"2nd Week Process ID",'
')),
splitLayout(cellWidths = c("25%","25%"),
numericInput(inputId = "week2startid",label = "Starting ID",value = 0,width = "100px"),
numericInput(inputId = "week2endid",label = "Finishing ID",value = 0,width = "100px")
),
HTML(paste('
',"3rd Week Process ID",'
')),
splitLayout(cellWidths = c("25%","25%"),
numericInput(inputId = "week3startid",label = "Starting ID",value = 0,width = "100px"),
numericInput(inputId = "week3endid",label = "Finishing ID",value = 0,width = "100px")
),
HTML(paste('
',"4th Week Process ID",'
')),
splitLayout(cellWidths = c("25%","25%"),
numericInput(inputId = "week4startid",label = "Starting ID",value = 0,width = "100px"),
numericInput(inputId = "week4endid",label = "Finishing ID",value = 0,width = "100px")
),
actionButton(inputId = "mprocess",label = "Click me!")

),
box(width = 8,
height = "350px",
tableOutput(outputId = "mdatatable")
)

)

server <- function(input, output, session) {
vvr <- reactiveValues(mydata=NULL)

observeEvent(input$mprocess,{
vvr$mydata <- data.frame(Process_ID = seq(from=65000,to=80000,by=100))
vvr$mydata$Week_ID <- 0

vvr$mydata$Week_ID <- ifelse(vvr$mydata$Process_ID >= input$week1startid & vvr$mydata$Process_ID < input$week1endid,1,
                             ifelse(vvr$mydata$Process_ID >= input$week2startid & vvr$mydata$Process_ID < input$week2endid,2,
                                    ifelse(vvr$mydata$Process_ID >= input$week3startid & vvr$mydata$Process_ID < input$week3endid,3,
                                           ifelse(vvr$mydata$Process_ID >= input$week4startid & vvr$mydata$Process_ID < input$week4endid,4,0 ))))

})

output$mdatatable <- renderTable({
vvr$mydata
})

}
shinyApp(ui, server)

If I understand your question correctly, you want to map Process_ID numbers to corresponding Week Numbers. For example, the Process_ID "74250" should map to the Week Number "1".

You can do this by using a named vector to define your mapping.
Then subsetting your mapping with the column that you want to transform.

mapping <- c("74250" = 1, "8000" = 2, "9000" = 3)
data$Week_Number <- mapping[as.character(data$Process_ID)]

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.