求助:使用R构建含元素出现顺序位置的矩阵
Solution to Build the Position Matrix in R
Let's break down how to implement exactly what you're asking for. First, let's recap the requirements to make sure we're on the same page:
- We slide a window of size
step=3over your data (moving one row at a time) - For each window, if any value in
column2is 1, we mark it as "Yes"; otherwise "No" - For each unique character in
column1, we fill in its position (1,2,3) if it's in the current window, else 0
Here's the complete code to achieve this:
# Load your sample data sampledata <- data.frame( "column1" = c("a","b","c","d","e","a","b","e"), "column2" = c(1,0,0,1,0,0,0,0) ) # Define parameters step <- 3 # Get all unique characters from column1 (add "f" here if you need it explicitly) unique_chars <- unique(sampledata$column1) # Number of sliding windows n_windows <- nrow(sampledata) - step + 1 # Function to process each window process_window <- function(window_start) { # Get the rows for this window window_rows <- window_start:(window_start + step - 1) # Check if the window has any 1 in column2 has_yes <- any(sampledata$column2[window_rows] == 1) yes_no_marker <- ifelse(has_yes, "Yes", "No") # Extract characters in the current window window_chars <- sampledata$column1[window_rows] # Create values for each unique character: position if present, else 0 position_values <- sapply(unique_chars, function(char) { pos <- which(window_chars == char) if (length(pos) > 0) pos else 0 }) # Combine position values with the marker c(position_values, Marker = yes_no_marker) } # Apply the function to all windows and convert to a data frame result_matrix <- do.call(rbind, lapply(1:n_windows, process_window)) # Convert position columns to integers (they're characters initially) result_matrix[, 1:length(unique_chars)] <- lapply(result_matrix[, 1:length(unique_chars)], as.integer) # Name the rows for clarity rownames(result_matrix) <- paste0("Window_", 1:n_windows) # View the final result print(result_matrix)
What this code does:
- Setup: We start by defining your sample data and parameters (window size
step=3). We also extract all unique characters fromcolumn1to use as columns in our result. - Window Processing Function: The
process_windowfunction handles each window:- It grabs the rows for the current window
- Checks if there's any 1 in
column2to set the "Yes/No" marker - For each unique character, it finds its position in the window (1,2,3) or uses 0 if it's not present
- Combine Results: We apply the function to every window, bind the results into a matrix, convert the position columns to integers, and add clear row names for each window.
Example Output:
When you run this code, you'll get a matrix that looks like this:
a b c d e Marker Window_1 1 2 3 0 0 Yes Window_2 0 1 2 3 0 Yes Window_3 0 0 1 2 3 Yes Window_4 3 0 0 1 2 Yes Window_5 2 3 0 0 1 No Window_6 1 2 0 0 3 No
If you need to include "f" as a column even though it's not in your sample data, just modify the unique_chars line to:
unique_chars <- c(unique(sampledata$column1), "f")
This will add an extra column where all values are 0, as requested in your initial description.
内容的提问来源于stack exchange,提问作者user080391
相关产品推荐
相关产品推荐

