Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.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 吐痰和分配身份_R_Split - Fatal编程技术网

R 吐痰和分配身份

R 吐痰和分配身份,r,split,R,Split,我需要在df中创建一个变量,根据拆分结果指定一个唯一的顺序值。我一直在搜索,发现split()可以帮助我。然而,我被困在如何分配顺序值 我的数据的简化形式如下 structure(list(Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L), Session = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "July", class = "factor

我需要在df中创建一个变量,根据拆分结果指定一个唯一的顺序值。我一直在搜索,发现split()可以帮助我。然而,我被困在如何分配顺序值

我的数据的简化形式如下

structure(list(Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
2014L, 2014L), Session = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = "July", class = "factor"), SiteName = structure(c(2L, 
2L, 1L, 1L, 4L, 4L, 3L, 3L), .Label = c("Kaoshe", "Matoa", 
"Livingi", "Sedina"), class = "factor"), Temp = c(23L, 12L, 15L, 
27L, 30L, 21L, 21L, 21L)), .Names = c("Year", "Session", "SiteName", 
"Temp"), class = "data.frame", row.names = c(NA, -8L))

我做了
temp我们可以使用
数据表中的
.GRP

library(data.table)
setDT(df)[, order := .GRP, .(SiteName, Session, Year)]

或使用
base R

df$order <- cumsum(!duplicated(df[1:3]))
df$order
#[1] 1 1 2 2 3 3 4 4

df$order我们可以使用
数据表中的
.GRP

library(data.table)
setDT(df)[, order := .GRP, .(SiteName, Session, Year)]

或使用
base R

df$order <- cumsum(!duplicated(df[1:3]))
df$order
#[1] 1 1 2 2 3 3 4 4

df$order是的,这解决了我的问题。非常感谢你,这解决了我的问题。多谢各位