简体   繁体   English

根据列观察值从数据中提取特定值

[英]Extracting specific values from data based on column observations

I am trying to extract specific values from the data based on some conditions (in year_col) column. 我试图基于某些条件(在year_col中)从数据中提取特定值。

Data: 数据:

   id_row year_row id_col year_col      value
1     ID1     2000    ID1     2000 0.00000000
2     ID1     2001    ID1     2000 0.01265282
3     ID1     2002    ID1     2000 0.97564274
4     ID1     2003    ID1     2000 0.98024541
5     ID1     2004    ID1     2000 0.51856337
6     ID2     2000    ID1     2000 1.00000000
7     ID2     2001    ID1     2000 0.97501220
8     ID2     2002    ID1     2000 0.97273607
9     ID3     2000    ID1     2000 1.00000000
10    ID1     2000    ID1     2001 0.01265282
11    ID1     2001    ID1     2001 0.00000000
12    ID1     2002    ID1     2001 0.97198829
13    ID1     2003    ID1     2001 0.97728153
14    ID1     2004    ID1     2001 0.49576573
15    ID2     2000    ID1     2001 1.00000000
16    ID2     2001    ID1     2001 0.97126315
17    ID2     2002    ID1     2001 0.96864553
18    ID3     2000    ID1     2001 1.00000000
19    ID1     2000    ID1     2002 0.97564274
20    ID1     2001    ID1     2002 0.97198829

Expected Output: 预期产量:

   id_row year_row id_col year_col      value

1     ID1     2001    ID1     2000 0.01265282
2     ID1     2002    ID1     2001 0.97198829 
3     ID1     2003    ID1     2002 0.09580557
4     ID1     2004    ID1     2003 0.94777670
5     ID2     2001    ID2     2000 0.97501220
6     ID2     2002    ID2     2001 0.96864553

That is extract the values for each ID in id_row and id_col and for each year in year_row, but with the lagged year_col. 那就是提取id_row和id_col中每个ID的值以及year_row中每个年份的值,但是带有滞后的year_col。

I have looked into the group_by and filter without much luck. 我调查了group_by并进行了过滤,但运气不佳。

Here is my data 这是我的数据

df <- structure(list(id_row = c("ID1", "ID1", "ID1", "ID1", "ID1", 
"ID2", "ID2", "ID2", "ID3", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID2", "ID2", "ID2", "ID3", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID2", "ID2", "ID2", "ID3", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID2", "ID2", "ID2", "ID3", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID2", "ID2", "ID2", "ID3", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID2", "ID2", "ID2", "ID3", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID2", "ID2", "ID2", "ID3", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID2", "ID2", "ID2", "ID3", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID2", "ID2", "ID2", "ID3"), year_row = c("2000", "2001", "2002", 
"2003", "2004", "2000", "2001", "2002", "2000", "2000", "2001", 
"2002", "2003", "2004", "2000", "2001", "2002", "2000", "2000", 
"2001", "2002", "2003", "2004", "2000", "2001", "2002", "2000", 
"2000", "2001", "2002", "2003", "2004", "2000", "2001", "2002", 
"2000", "2000", "2001", "2002", "2003", "2004", "2000", "2001", 
"2002", "2000", "2000", "2001", "2002", "2003", "2004", "2000", 
"2001", "2002", "2000", "2000", "2001", "2002", "2003", "2004", 
"2000", "2001", "2002", "2000", "2000", "2001", "2002", "2003", 
"2004", "2000", "2001", "2002", "2000", "2000", "2001", "2002", 
"2003", "2004", "2000", "2001", "2002", "2000"), id_col = c("ID1", 
"ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", 
"ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID1", "ID2", 
"ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", 
"ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", 
"ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID2", "ID3", 
"ID3", "ID3", "ID3", "ID3", "ID3", "ID3", "ID3", "ID3"), year_col = c("2000", 
"2000", "2000", "2000", "2000", "2000", "2000", "2000", "2000", 
"2001", "2001", "2001", "2001", "2001", "2001", "2001", "2001", 
"2001", "2002", "2002", "2002", "2002", "2002", "2002", "2002", 
"2002", "2002", "2003", "2003", "2003", "2003", "2003", "2003", 
"2003", "2003", "2003", "2004", "2004", "2004", "2004", "2004", 
"2004", "2004", "2004", "2004", "2000", "2000", "2000", "2000", 
"2000", "2000", "2000", "2000", "2000", "2001", "2001", "2001", 
"2001", "2001", "2001", "2001", "2001", "2001", "2002", "2002", 
"2002", "2002", "2002", "2002", "2002", "2002", "2002", "2000", 
"2000", "2000", "2000", "2000", "2000", "2000", "2000", "2000"
), value = c(0, 0.0126528243223428, 0.975642738186892, 0.980245408067008, 
0.518563371872083, 1, 0.975012198097823, 0.97273607293636, 1, 
0.0126528243223428, 0, 0.971988294886507, 0.977281526630117, 
0.495765729632059, 1, 0.971263151675716, 0.968645527933215, 1, 
0.975642738186892, 0.971988294886507, 0, 0.0958055698205349, 
0.952303016080655, 0.771039058422751, 0.71926736774858, 0.875209312956793, 
0.761542337380287, 0.980245408067008, 0.977281526630117, 0.0958055698205349, 
0, 0.947776703213291, 0.758596060369833, 0.721719565905183, 0.875788199318376, 
0.742136651511878, 0.518563371872083, 0.495765729632059, 0.952303016080655, 
0.947776703213291, 0, 0.940736749815103, 0.937612053309506, 0.975975000994785, 
0.943889690922876, 1, 1, 0.771039058422751, 0.758596060369833, 
0.940736749815103, 0, 0.753367507803825, 0.820601259631019, 0.0572833227621783, 
0.975012198097823, 0.971263151675716, 0.71926736774858, 0.721719565905183, 
0.937612053309506, 0.753367507803825, 0, 0.903984128296163, 0.765562291938692, 
0.97273607293636, 0.968645527933215, 0.875209312956793, 0.875788199318376, 
0.975975000994785, 0.820601259631019, 0.903984128296163, 0, 0.844300211167695, 
1, 1, 0.761542337380287, 0.742136651511878, 0.943889690922876, 
0.0572833227621783, 0.765562291938692, 0.844300211167695, 0)), class = "data.frame", row.names = c(NA, 
-81L))

you could use the dplyr package: 您可以使用dplyr软件包:

library(dplyr)
df %>% 
  filter(id_row == id_col, as.numeric(year_row) == as.numeric(year_col) + 1)

    id_row year_row id_col year_col      value
1    ID1     2001    ID1     2000 0.01265282
2    ID1     2002    ID1     2001 0.97198829
3    ID1     2003    ID1     2002 0.09580557
4    ID1     2004    ID1     2003 0.94777670
5    ID2     2001    ID2     2000 0.75336751
6    ID2     2002    ID2     2001 0.90398413

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

相关问题 根据其他列从列中提取特定值 - Extracting specific values from column based on other column 根据条件从R中的另一个数据表中提取列值 - Extracting column values based on condition from another data table in R 从R中的文本列中提取特定数据 - Extracting specific data from text column in R 基于符号从列中提取数据 - Extracting Data from a column based on symbols 在分组数据帧内,基于另一列中多个观测值的比较,有条件地将值分配给该列中的NA - Conditionally assign values to NAs in a column based on comparison of multiple observations in another column, within a grouped data frame 在提取特定列值时应用分组和汇总数据 - Applying group by and summarise data while extracting specific column values 根据其他列提取值 - Extracting values based on other column 如何根据列名的部分字符串匹配从列中选择特定观察 - How to select specific observations from columns based on partial string match of column names 根据行值从数据框中的不同列中提取数据 - Extracting data from different columns in a data frame based on row values 根据数据集 B 的列中的观察数复制数据集 A,其中两个数据集在 1 列中具有匹配值 - Replicating data set A based on the number of observations in the column of data set B where both data sets have a matching values in 1 column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM