R 从列表中的每个项目和其他问题中提取第二个元素
以下是我试图做的: 这让我看到了更多的光明,但这就是我想说的。假设您有一个如下所示的数据——R 从列表中的每个项目和其他问题中提取第二个元素,r,R,以下是我试图做的: 这让我看到了更多的光明,但这就是我想说的。假设您有一个如下所示的数据—— Region Open Store 120..141 + France 145..2115 + Germany 3322..5643 + Wales 5646..7451 - Scotland 7454..8641 - Mexico 8655..9860 - India 9980..11413
Region Open Store
120..141 + France
145..2115 + Germany
3322..5643 + Wales
5646..7451 - Scotland
7454..8641 - Mexico
8655..9860 - India
9980..11413 + Zambia
11478..1261 - Nicaragua
12978..1318 + Sweeden
我试图做的是找出第二个元素(141)和连续的第一个元素(145)之间的差异,如果它们满足某个值并且具有相同的符号(+或-),则将商店分组在一起。输出示例
期望的输出应该是这样的(如果数字差小于40,并且存储标志相同(具有相同的+或-)
在“字符”向量的情况下,“第二个元素”(不是真正的元素)的向量是sapply(strsplit(dat$Region,\\\\\.\”),[”,2)
和“第一个元素”sapply(strsplit(dat$Region,\\.\”,[”,1)
。大概是第一个这样的差异(在data.frame
作为默认因子类的第一列的情况下)为:
[注意:“\.\”作为“split”参数的必要性来自于“split”参数被解释为正则表达式的事实。]所有差异的向量:
as.numeric(sapply( strsplit(as.character(dat$Region), "\\.\\.") , "[", 1)[-1]) -
as.numeric(sapply( strsplit(as.character(dat$Region), "\\.\\.") , "[", 1)[-length(dat$Region)])
[1] 25 3177 2324 1808 1201 1325 1498 1500
你剩下的问题(以及编辑)没有传达你的意图(可能是因为缺乏共同的自然语言)。(我不知道“他们所有的商店”和“商店标志”可能是什么意思。)请努力用IDiomatic语言进行交流。这与您提供的数据相符-我是R的一个无事可做的人,如果它很混乱,很抱歉
# Split the string in the first column to make it easier to compare
library(stringr)
regionl<-str_split_fixed(data$Region,c("[..]"),3)[,1]
regionr<-str_split_fixed(data$Region,c("[..]"),3)[,3]
data$regionl <- regionl
data$regionr <-regionr
# We set a threshold for comparison
threshold = 100
# Lets loop through the data and check the right column with the left column
# We see if it is less than the threshold and has the same sign
# We add the groups up until there is a discrepancy and we print
currentGroup = NULL
for(i in 1:(nrow(data)-1))
{
# Boolean variables checking against signs and thresholds
difference <- abs(as.numeric(data$regionr[i])-as.numeric(data$regionl[i+1])) <= threshold
signs <- (data$Open[i] == data$Open[i+1])
# Group things together
if(difference & signs)
{
currentGroup <- c(currentGroup,as.character(data$Store[i]),as.character(data$Store[i+1]))
}
else
{
# If it's in a group alone, do not print
if(is.null(currentGroup))
{
# Do nothing
}else
{
# Print groups
print(unique(currentGroup))
}
# Reset the group holder
currentGroup<-NULL
}
}
#拆分第一列中的字符串以便于比较
图书馆(stringr)
区域“连续第一个元素”是指145?你能在问题中添加所需的输出吗?是的,我的意思是145,我希望额外的信息有助于你完全理解。你能更改所需的输出,使其格式与你想要的完全相同吗?现在,它确实不能帮助任何人理解你的问题。新格式有帮助吗?我添加了一些更多的解释。我希望这有助于你的计算应该是第二个元素和连续的第一个元素之间的差异。我可以看到你的输出计算了120和145之间的差异。它应该是141和145,2115和3322等等,作为你的输入。这是有效的,但不是我尝试的大数据集在您的代码示例中,返回的存储不应为Hi!当然,您可以为我们提供数据集中未产生预期结果的部分吗?PS。您可以将阈值更改为您喜欢的任何值~给出问题的是“存储”。currentGroup
as.numeric(sapply( strsplit(as.character(dat$Region), "\\.\\.") , "[", 1)[-1]) -
as.numeric(sapply( strsplit(as.character(dat$Region), "\\.\\.") , "[", 1)[-length(dat$Region)])
[1] 25 3177 2324 1808 1201 1325 1498 1500
# Split the string in the first column to make it easier to compare
library(stringr)
regionl<-str_split_fixed(data$Region,c("[..]"),3)[,1]
regionr<-str_split_fixed(data$Region,c("[..]"),3)[,3]
data$regionl <- regionl
data$regionr <-regionr
# We set a threshold for comparison
threshold = 100
# Lets loop through the data and check the right column with the left column
# We see if it is less than the threshold and has the same sign
# We add the groups up until there is a discrepancy and we print
currentGroup = NULL
for(i in 1:(nrow(data)-1))
{
# Boolean variables checking against signs and thresholds
difference <- abs(as.numeric(data$regionr[i])-as.numeric(data$regionl[i+1])) <= threshold
signs <- (data$Open[i] == data$Open[i+1])
# Group things together
if(difference & signs)
{
currentGroup <- c(currentGroup,as.character(data$Store[i]),as.character(data$Store[i+1]))
}
else
{
# If it's in a group alone, do not print
if(is.null(currentGroup))
{
# Do nothing
}else
{
# Print groups
print(unique(currentGroup))
}
# Reset the group holder
currentGroup<-NULL
}
}