[英]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_A
、 Name_B
和Name_C
)上得分。 These scores are currently recorded against the associated variable horizontally in the X1Score
, X2Score
, and X3Score
columns.这些分数目前是根据X1Score
、 X2Score
和X3Score
列中的相关变量水平记录的。 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_A
、 Name_B
和Name_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.