mat <- structure(list(c(1, 2, 3, 4, 5), 2, c(3, 2, 1), numeric(0), numeric(0),
numeric(0), c(1, 2, 3, 6), c(1, 2, 3, 4, 5), 1, numeric(0),
numeric(0), numeric(0), c(3, 4, 2), 3, c(1, 2, 3, 4, 5),
numeric(0), numeric(0), numeric(0), numeric(0), numeric(0),
numeric(0), 1.358, numeric(0), numeric(0), numeric(0), numeric(0),
numeric(0), numeric(0), 0.0223257970827299, numeric(0), numeric(0),
numeric(0), numeric(0), numeric(0), numeric(0), 1.493), .Dim = c(6L,
6L))
> mat
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] Numeric,5 Numeric,4 Numeric,3 Numeric,0 Numeric,0 Numeric,0
[2,] 2 Numeric,5 3 …Run Code Online (Sandbox Code Playgroud) 下面是一个数据框,df它有1个变量ID,500K数据点,我需要实现一个event counter具有以下条件.
1.增量 event counter当ID == A
2.但是,不应考虑前3个数据点用于计数器增量ID == A.
下面显示了df具有预期输出 的数据框
ID Event Counter
D 0
F 0
V 0
A 0
A 0
A 0
A 1
A 1
A 1
V 1
F 1
A 1
A 1
A 1
A 2
F 2
G 2
A 2
A 2
A 2
A 3
A 3
Run Code Online (Sandbox Code Playgroud)
请注意: - 行号1,2和3不满足条件,因此没有增量Event Counter.虽然ID …
我在R中有以下数据框
Service Container_Pick_Day
ABC 0
ABC 1
ABC 1
ABC 2
ABC NA
ABC 0
ABC 1
DEF NA
DEF 0
DEF 1
DEF 1
DEF 1
DEF 2
DEF 1
Run Code Online (Sandbox Code Playgroud)
列Container_Pick_Day是数字,由NA值组成。我想做的是计算忽略值Service的容器的明智百分比0th day,after 1 day,2 day and so onNA
所需的数据帧将是
Service Container_Pick_Day Percentage
ABC 0 (2/6)*100 = 33.33
ABC 1 (3/6)*100 = 50
ABC 2 (1/6)*100 = 16.67
DEF 0 (1/6)*100 = 16.67
DEF 1 (3/6)*100 = 50
DEF 2 (1/6)*100 = …Run Code Online (Sandbox Code Playgroud) 我有一个泛型函数,我想从中获取三个值。但是,它仅返回“返回”功能中的值之一。我的代码结构如下:
doEverythingFunction <- function(x){
"do some esoteric calculations here"
return(valueX)
"do some more esoteric calculations here"
return(valueY)
"do even more esoteric calculations here"
return(valueZ)
}
Run Code Online (Sandbox Code Playgroud)
此函数仅返回Z值,而不返回X和Y。如何获取所有三个值?
我需要用$ Country的因子填充缺少序列值的$ Year.$ Count列可以用0填充.
Country Year Count
A 1 1
A 2 1
A 4 2
B 1 1
B 3 1
Run Code Online (Sandbox Code Playgroud)
所以我最终得到了
Country Year Count
A 1 1
A 2 1
A 3 0
A 4 2
B 1 1
B 2 0
B 3 1
Run Code Online (Sandbox Code Playgroud)
希望这是明确的家伙,提前谢谢!
如果我有一个数据集工作,我想对数据进行分组(即通过country),计算汇总统计(mean()),然后ungroup()将data.frame有一个与原始尺寸的数据集(country- year)和一个新列,其中列出了平均每个国家(重复超过n年),我该怎么做dplyr?该ungroup()函数不返回data.frame原始尺寸:
gapminder %>%
group_by(country) %>%
summarize(mn = mean(pop)) %>%
ungroup() # returns data.frame with nrows == length(unique(gapminder$country))
Run Code Online (Sandbox Code Playgroud) 就像数据帧中的列和X1 $ value + X2 $值一样,我想在尽可能少的R代码的列表列表上执行它,而不是使用for-loop.谢谢.
输入:
n1 <- c(1,2,3)
n2 <- c(2,3,4)
df1 <- data.frame(n1,n2)
n1 <- c(3,2,1)
n2 <- c(5,5,6)
df2 <- data.frame(n1,n2)
mylist <- list(df1, df2)
n0 <- c(0,0,0) #init compute
compute <- data.frame(n0)
for(i in 1:length(mylist)){
compute[,1] <- mylist[[i]]$n1 + compute[,1]
}
Run Code Online (Sandbox Code Playgroud)
期望的输出:
>compute
n0
1 4
2 4
3 4
Run Code Online (Sandbox Code Playgroud) 我需要能够在 R 中反转单词。例如,将“这是我的文本”转换为“我的文本是这个”。我尝试使用 stringr 包中的 word 函数,如下所示,但没有用,只有“”
word("this is my text", -1,1)
[1] ""
Run Code Online (Sandbox Code Playgroud)
任何建议为什么上述方法不起作用或任何其他方式来反转单词?
我正在尝试将季度收益转换为年度收益。给定一个季度回报向量,该怎么做?
我是R编程的新手,所以我真的一无所获。
给定一个a包含季度收益的向量:
a <- c(0.11, 0.02, 0.01, 0.1, 0.08, 0.04, 0.02, 0.03) # Two years worth of returns
Run Code Online (Sandbox Code Playgroud)
我想应用一些函数,该函数使用公式输出带有年收益的长度为2的向量:
Year 1: ((1 + 0.11) * (1 + 0.02) * (1 + 0.01) * (1 + 0.1))-1 = 0,2578742
Year 2: ((1 + 0.08) * (1 + 0.04) * (1 + 0.02) * (1 + 0.03))-1 = 0,180033
Run Code Online (Sandbox Code Playgroud)
最终向量:
yearly_vec <- c(0.2578742, 0.180033)
Run Code Online (Sandbox Code Playgroud) 我想根据该 ID 本身的前几年组的值在组变量中填充我的数据集的 NA。该na.locf(newData, na.rm = TRUE)部分代码不能正常工作。我认为这是因为输入不是数字。或者是另一回事?有谁知道如何解决这个问题?
for (i in my_data$ID){
newData = my_data[my_data$ID==i,c('ID','Year', 'group')][3]
na.locf(newData,na.rm = TRUE)
}
Run Code Online (Sandbox Code Playgroud)
我的数据集非常大。但我提供了这个作为我需要的样本:
structure(list(ID = c(1L, 2L, 3L, 1L, 1L, 1L), Year = c(2000L,
2000L, 2001L, 2001L, 2002L, 2003L), Group = structure(c(2L, 3L,
2L, 1L, 1L, 4L), .Label = c("", "\"A\"", "\"B\"", "\"C\""), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
结果应该是这样的:
structure(list(ID = c(1L, 1L, 1L, 1L, 2L, 2L), Year = c(2000L,
2001L, 2002L, 2003L, 2000L, 2002L), Group …Run Code Online (Sandbox Code Playgroud)