gpt4 book ai didi

r - 循环 r 中的字符串

转载 作者:行者123 更新时间:2023-12-01 13:40:43 24 4
gpt4 key购买 nike

我想知道我的代码有什么问题而不是解决方案。我希望遍历一些字符串我的数据如下:

id    source    transaction

1 a > b 6 > 0
2 J > k 5
3 b > c 4 > 0

我有一个列表,希望查看此列表并找到包含该元素的行并计算平均值。
mylist <- c ("a", "b") 

所以我想要的输出列表中的元素之一是
source  avg
a 6
b 2

我不知道谁来遍历列表并将它们发送到 csv 文件。我试过这个
mylist <- c( "a", "b" )

for(i in mylist)
{

KeepData <- df [grepl(i, df$source), ]
KeepData <- cSplit(KeepData, "transaction", ">", "long")

avg<- mean(KeepData$transactions)
result <- list(i,avg )

write.table(result ,file="C:/Users.csv", append=TRUE,sep=",",col.names=FALSE,row.names=FALSE)

}

但它给了我“NA”结果并带有以下警告

Warning messages: 1: In mean.default(KeepData$transactions) :
argument is not numeric or logical: returning NA 2: In mean.default(KeepData$transactions) : argument is not numeric or logical: returning NA

最佳答案

我们可以使用 cSplit拆分“源”并将数据集转换为“长”格式,然后指定“i”,按“源”分组,得到mean '交易'(使用 data.table 方法)

library(splitstackshape)
cSplit(df1, "source", " > ", "long")[source %in% mylist, .(avg = mean(transaction)), source]
# source avg
#1: a 6
#2: b 5

或者另一个选项是 separate_rows来自 tidyr转换为“长”格式,然后使用 dplyr方法 summarise按“来源”分组后
library(tidyr)
library(dplyr)
separate_rows(df1, source) %>%
filter(source %in% mylist) %>%
group_by(source) %>%
summarise(avg = mean(transaction))

更新

对于新数据集('df2'),我们需要将两列拆分为'long'格式,然后得到 mean按“来源”分组的“交易”
cSplit(df2, 2:3,  " > ", "long")[source %in% my_list, .(avg = mean(transaction)), source]
# source avg
#1: a 6
#2: b 2
for循环可以修改为
for(i in mylist) {
KeepData <- cSplit(df2, 2:3, ">", "long")
KeepData <- KeepData[grepl(i, source)]
avg<- mean(KeepData$transaction)
result <- list(i,avg )
print(result)
write.table(result ,file="C:/Users.csv",
append=TRUE,sep=",",col.names=FALSE,row.names=FALSE)
}
#[[1]]
#[1] "a"

#[[2]]
#[1] 6

#[[1]]
#[1] "b"

#[[2]]
#[1] 2

数据
df1 <- structure(list(id = 1:3, source = c("a > b", "J > k", "b > c"
), transaction = c(6L, 5L, 4L)), .Names = c("id", "source", "transaction"
), class = "data.frame", row.names = c(NA, -3L))


df2 <- structure(list(id = 1:3, source = c("a > b", "J > k", "b > c"
), transaction = c("6 > 0", "5", "4 > 0")), .Names = c("id",
"source", "transaction"), class = "data.frame", row.names = c(NA,
-3L))

关于r - 循环 r 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40539981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com