简体   繁体   English

在 R 中,如何根据特定的行/列条件有选择地将一个单元格“复制并粘贴”到另一个单元格中?

[英]In R, how do I selectively 'copy and paste' a cell into another cell based on specific row/column criteria?

I have some data (see below) whereby I have participants ( ID column) who have a score on three variables ( Name_A , Name_B , and Name_C ).我有一些数据(见下文),其中我有参与者( ID列)在三个变量( Name_AName_BName_C )上得分。 These scores are currently recorded against the associated variable horizontally in the X1Score , X2Score , and X3Score columns.这些分数目前是根据X1ScoreX2ScoreX3Score列中的相关变量水平记录的。 I would like these scores 'copied and pasted' (for lack of a better phrase) to the associated columns – being Name_A , Name_B , and Name_C (currently filled with NA ) – so that I have the data in long format.我希望将这些分数“复制并粘贴”(因为缺少更好的短语)到相关列 - Name_AName_BName_C (当前填充为NA ) - 这样我就有了长格式的数据。 How do I do this?我该怎么做呢?

ID  X1      X1Score X2      X2Score X3      X3Score Name_A    Name_B  Name_C
1   Name_A  4.58    Name_C  4.79    Name_B  5.22    NA        NA      NA
2   Name_C  5.35    Name_B  5.33    Name_A  5.61    NA        NA      NA
3   Name_B  5.59    Name_C  5.48    Name_A  4.89    NA        NA      NA
4   Name_C  5.36    Name_B  5.04    Name_A  4.93    NA        NA      NA
5   Name_A  5.39    Name_B  5.27    Name_C  5.11    NA        NA      NA
6   Name_C  4.91    Name_A  4.99    Name_B  5.01    NA        NA      NA



df <- structure(list(ID = 1:6,
                         X1 = c("Name_A", "Name_C", "Name_B", "Name_C", "Name_A", "Name_C"),
                         X1Score = c(4.58, 5.35, 5.59, 5.36, 5.39, 4.91),
                         X2 = c("Name_C", "Name_B", "Name_C", "Name_B", "Name_B", "Name_A"),
                         X2Score = c(4.79, 5.33, 5.48, 5.04, 5.27, 4.99),
                         X3 = c("Name_B", "Name_A", "Name_A", "Name_A", "Name_C", "Name_B"),
                         X3Score = c(5.22, 5.61, 4.89, 4.93, 5.11, 5.01),
                         Name_A = c(NA, NA, NA, NA, NA, NA),
                         Name_B = c(NA, NA, NA, NA, NA, NA),
                         Name_C = c(NA, NA, NA, NA, NA, NA)),
                    row.names = c(NA, -6L), class = "data.frame")

#Edit: My original request above is too simplistic and, although the answer technically addressed the question, I failed to understand how to generalise it. #Edit:我上面的原始请求太简单了,虽然答案在技术上解决了这个问题,但我无法理解如何概括它。 So, here is a revised example (where the only major difference is the naming convention of the columns) - this example produces an error, despite the same code working on the above example.因此,这是一个修改后的示例(其中唯一的主要区别是列的命名约定) - 尽管在上面的示例中使用相同的代码,但此示例会产生错误。 My hope is that with another example of my problem, I will be able to make sense of the 'X\\\\d+(.*)' line as it looks like this is the key to making it work.我希望通过我的问题的另一个例子,我将能够理解'X\\\\d+(.*)'行,因为它看起来是让它工作的关键。 Here is the updated example:这是更新的示例:

df <- structure(list(ID = 1:6,
                     X1_Name = c("Name_A", "Name_C", "Name_B", "Name_C", "Name_A", "Name_C"),
                     X1_Score = c(4.58, 5.35, 5.59, 5.36, 5.39, 4.91),
                     X5_Name = c("Name_C", "Name_B", "Name_C", "Name_B", "Name_B", "Name_A"),
                     X5_Score = c(4.79, 5.33, 5.48, 5.04, 5.27, 4.99),
                     X19_Name = c("Name_B", "Name_A", "Name_A", "Name_A", "Name_C", "Name_B"),
                     X19_Score = c(5.22, 5.61, 4.89, 4.93, 5.11, 5.01)),
                row.names = c(NA, -6L), class = "data.frame")

df %>%
  #get the data in long format creating two columns Name and Score
  pivot_longer(cols = -ID, 
               names_to = '.value',
               names_pattern = 'X\\d+(.*)') %>%
  #Get data in wide format. 
  pivot_wider(names_from = Name, values_from = Score)

You may perform reshaping with pivot_longer / pivot_wider -您可以使用pivot_longer / pivot_wider执行整形 -

library(dplyr)
library(tidyr)

df %>%
  #To drop empty NA columns
  select(-starts_with('Name')) %>%
  #Rename X1 to X1Name, X2 to X2Name and so on
  rename_with(~paste0(., 'Name'), matches('^X\\d+$')) %>%
  #get the data in long format creating two columns Name and Score
  pivot_longer(cols = -ID, 
               names_to = '.value',
               names_pattern = 'X\\d+(.*)') %>%
  #Get data in wide format. 
  pivot_wider(names_from = Name, values_from = Score)

#     ID Name_A Name_C Name_B
#  <int>  <dbl>  <dbl>  <dbl>
#1     1   4.58   4.79   5.22
#2     2   5.61   5.35   5.33
#3     3   4.89   5.48   5.59
#4     4   4.93   5.36   5.04
#5     5   5.39   5.11   5.27
#6     6   4.99   4.91   5.01

If you want to keep all other columns in the data as it is and add these 3 columns separately you may join the dataset with the original one.如果您想保留数据中的所有其他列并分别添加这 3 列,您可以将数据集与原始数据集连接起来。

...Code from above %>%
   left_join(df %>% select(-starts_with('Name')), by = 'ID')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据R中同一行的另一列中的单元格的值对一列求和 - Sum a column based on the value of a cell in another column of the same row in R 如何从一个数据框中的两个条件中选择一个值,然后将其粘贴到另一个数据框中的单元格中? - How do I select a value based on two conditions from a data frame and then paste it into a cell in another data frame? 根据该行中另一个单元格的值更改 dataframe 单元格中的值 (R) - Changing a value in a dataframe cell based on the value of another cell in that row (R) (R) 如何根据 R 中的另一列和 ID 从一列复制粘贴值 - (R) How to copy paste values from one column based on another column and ID in R 如何根据 R 中的单元格字符格式化 xlsx 中的整行 - How do I format an entire row within xlsx based on a cell character in R 如何完全删除R中具有空单元格的行? - How do I completely delete a row with an empty cell in R? 如何在 R 中复制一列并将值粘贴到另一列下方? - How Do You Copy A Column and Paste Values Below Another Column in R? 如何对矩阵中的列求和并将值粘贴到 r 的最后一个单元格中? - How to sum a column in a matrix and paste the value in the last cell in r? 如果一行符合条件,请粘贴 R - If a row matches a criteria do paste in R 如何基于R中的条件将单元格的值传播到其他行 - How to propagate value of a cell to other rows based on criteria in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM