You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中基于语音特征矩阵计算目标与实际发音的特征距离

Solution: Calculating Feature Differences with a For Loop in 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) and C1_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.matrix uses meaningful column names (like voice, bilabial, etc.), replace diff_col_names with paste0(colnames(distinctive.feature.matrix), "_diff") to make the output columns more readable.
  • If you have missing values (NA) in C1_target or C1_actual, the loop will skip those rows (since NA %in% rownames(...) returns FALSE).
  • If you want to avoid for loops entirely (R often prefers vectorized operations), you could use dplyr with rowwise()—but since you specifically asked for a for loop, this solution sticks to that approach.

内容的提问来源于stack exchange,提问作者Catherine Laing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:05:50