Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在r中使用for循环标记大量数据点?_R_Loops_Label - Fatal编程技术网

如何在r中使用for循环标记大量数据点?

如何在r中使用for循环标记大量数据点?,r,loops,label,R,Loops,Label,我有一个数据集,其中包含与ID关联的ID和Address。例如: ID Address 1001 123 E example rd, 12300 1001 123 E example rd, 12300 1001 456 W example rd, 45600 1002 789 N example rd, 78900 1002 123 E example rd, 12300 1003 789 N example rd, 78900 1004 456 W example rd, 45600 1

我有一个数据集,其中包含与
ID
关联的
ID
Address
。例如:

ID   Address
1001 123 E example rd, 12300
1001 123 E example rd, 12300
1001 456 W example rd, 45600
1002 789 N example rd, 78900
1002 123 E example rd, 12300
1003 789 N example rd, 78900
1004 456 W example rd, 45600
1004 789 N example rd, 78900
1004 789 N example rd, 78900
1004 123 E example rd, 12300
现在,在上面的示例中,我们有3个唯一的ID。我想将它们标记为位置1、位置2和位置3。最后,我想要一个如下的数据结构:

ID     x1        x2        x3          x4 
1001   Place 1   Place 1   Place 2
1002   Place 3   Place 1
1003   Place 3
1004   Place 2   Place 3   Place 3     Place 1

因为在我的真实数据集中,我有大约3000个唯一的地址,所以我正在寻找能够循环并标记从第1位到第3000位的所有3000个地址的代码。

我们可以使用
匹配
唯一
将唯一地址替换为
“Place”
+后缀值,为每个
ID
创建一个唯一的索引,并使用
pivot\u wide
以宽格式获取数据

library(dplyr)

df1 <- df %>%
  mutate(Address = paste0('Place', match(Address, unique(Address)))) %>%
  group_by(ID) %>%
  mutate(row = paste0('x', row_number())) %>%
  tidyr::pivot_wider(names_from = row, values_from = Address)

df1

#    ID   x1     x2     x3     x4    
#  <int> <chr>  <chr>  <chr>  <chr> 
#1  1001 Place1 Place1 Place2 NA    
#2  1002 Place3 Place1 NA     NA    
#3  1003 Place3 NA     NA     NA    
#4  1004 Place2 Place3 Place3 Place1
数据

df <- structure(list(ID = c(1001L, 1001L, 1001L, 1002L, 1002L, 1003L, 
1004L, 1004L, 1004L, 1004L), Address = structure(c(1L, 1L, 2L, 
3L, 1L, 3L, 2L, 3L, 3L, 1L), .Label = c("123 E example rd, 12300", 
"456 W example rd, 45600", "789 N example rd, 78900"), class = "factor")), 
class = "data.frame", row.names = c(NA, -10L))

df我们可以使用
match
unique
将唯一地址替换为
“Place”
+后缀值,为每个
ID
创建唯一索引,并使用
pivot\u wide
以宽格式获取数据

library(dplyr)

df1 <- df %>%
  mutate(Address = paste0('Place', match(Address, unique(Address)))) %>%
  group_by(ID) %>%
  mutate(row = paste0('x', row_number())) %>%
  tidyr::pivot_wider(names_from = row, values_from = Address)

df1

#    ID   x1     x2     x3     x4    
#  <int> <chr>  <chr>  <chr>  <chr> 
#1  1001 Place1 Place1 Place2 NA    
#2  1002 Place3 Place1 NA     NA    
#3  1003 Place3 NA     NA     NA    
#4  1004 Place2 Place3 Place3 Place1
数据

df <- structure(list(ID = c(1001L, 1001L, 1001L, 1002L, 1002L, 1003L, 
1004L, 1004L, 1004L, 1004L), Address = structure(c(1L, 1L, 2L, 
3L, 1L, 3L, 2L, 3L, 3L, 1L), .Label = c("123 E example rd, 12300", 
"456 W example rd, 45600", "789 N example rd, 78900"), class = "factor")), 
class = "data.frame", row.names = c(NA, -10L))

df您能用R代码发布您的数据集的一个小示例吗?您可以使用本欢迎帖中所述的dput()!ID和地址在单独的列中还是在一列中?@Mohanasundaram,是的,ID和地址在单独的列中。你能用R代码发布一个数据集的小示例吗?您可以使用本欢迎帖中所述的dput()!ID和地址是在单独的列中还是在单独的列中?@Mohanasundaram,是的ID和地址在单独的列中如何从该输出将答案导出到CSV文件?如何从该输出将答案导出到CSV文件?