在R中基于语音特征矩阵计算目标与实际发音的特征距离
Got it, let's walk through how to build this for loop step by step. First, let's align on the expected structure of your datasets (since this will make the code work smoothly):
distinctive.feature.matrix: A data frame or matrix where rows are consonants (with row names matching the consonant labels in your production data) and columns are the 11 distinctive features (e.g.,voice,place, etc.).production.data: A data frame with at least two columns:C1_target(the intended consonant) andC1_actual(the child's produced consonant).
Step 1: Prepare Your Output Columns
First, let's add 11 new columns to production.data to store the feature differences. We'll name them something like feat1_diff to feat11_diff (you can rename these to match your actual feature names if you want):
# Create empty columns for feature differences num_features <- ncol(distinctive.feature.matrix) diff_col_names <- paste0("feat", 1:num_features, "_diff") production.data[diff_col_names] <- NA
Step 2: Build the For Loop
Now we'll loop through each row of production.data, pull the target and actual consonant features, compute the difference, and assign it to the new columns. We'll also add a quick check to skip rows where either consonant isn't present in the feature matrix (to avoid errors):
# Loop through each row in production.data for (i in 1:nrow(production.data)) { # Extract target and actual consonants for the current row target_c <- production.data$C1_target[i] actual_c <- production.data$C1_actual[i] # Check if both consonants exist in the feature matrix's row names if (target_c %in% rownames(distinctive.feature.matrix) && actual_c %in% rownames(distinctive.feature.matrix)) { # Pull the feature vectors for target and actual consonants target_feats <- distinctive.feature.matrix[target_c, ] actual_feats <- distinctive.feature.matrix[actual_c, ] # Calculate target - actual for each feature feat_diffs <- target_feats - actual_feats # Assign the differences to the corresponding row in production.data production.data[i, diff_col_names] <- feat_diffs } else { # Optional: Print a warning if a consonant is missing warning(paste("Row", i, ": Consonant", ifelse(!target_c %in% rownames(distinctive.feature.matrix), target_c, actual_c), "not found in feature matrix")) } }
Step 3: Verify the Results
After running the loop, you can check the first few rows to make sure everything worked:
head(production.data[, c("C1_target", "C1_actual", diff_col_names)])
Notes for Edge Cases
- If your
distinctive.feature.matrixuses meaningful column names (likevoice,bilabial, etc.), replacediff_col_nameswithpaste0(colnames(distinctive.feature.matrix), "_diff")to make the output columns more readable. - If you have missing values (
NA) inC1_targetorC1_actual, the loop will skip those rows (sinceNA %in% rownames(...)returnsFALSE). - If you want to avoid for loops entirely (R often prefers vectorized operations), you could use
dplyrwithrowwise()—but since you specifically asked for a for loop, this solution sticks to that approach.
内容的提问来源于stack exchange,提问作者Catherine Laing

