我正在操纵我的数据,发现我在这个过程中的某些方面做错了.当我探讨这个问题,这个问题归结为以下行为spread()的tidyr包.
这是一个示范性的例子.我们假设我们有一个如下数据框.
> d <- data.frame(factor1 = rep(LETTERS[1:3], each = 3),
+ factor2 = rep(paste0("level", c(1, 2, 10)), 3),
+ num = 1:9
+ )
> d
factor1 factor2 num
1 A level1 1
2 A level2 2
3 A level10 3
4 B level1 4
5 B level2 5
6 B level10 6
7 C level1 7
8 C level2 8
9 C level10 9
Run Code Online (Sandbox Code Playgroud)
我想要做的是将这个长格式的数据帧转换为宽格式.我认为这spread()是一种方法.然而,结果并不是我的预期.
> spread(d, factor2, num)
factor1 level1 level2 …Run Code Online (Sandbox Code Playgroud) 我有一个因子列.我想为每个因素分成一列,然后按每个id显示的因子计数填补空白.假设我们有:
car <- c("a","b","b","b","c","c","a","b","b","b","c","c")
type <- c("good", "regular", "bad","good", "regular", "bad","good", "regular", "bad","good", "regular", "bad")
car_type <- data.frame(car,type)
Run Code Online (Sandbox Code Playgroud)
得到:
car type
1 a good
2 b regular
3 b bad
4 b good
5 c regular
6 c bad
7 a good
8 b regular
9 b bad
10 b good
11 c regular
12 c bad
Run Code Online (Sandbox Code Playgroud)
我要这个:
> results
car good regular bad
1 a 2 0 0
2 b 2 2 2
3 c 0 2 2
Run Code Online (Sandbox Code Playgroud)
我尝试使用dplyr,但我并没有真正使用它,所以它不起作用. …
我在 Python 中缺少从长到宽的自发和轻松转换,反之亦然。想象一下,我有一个大而整洁的数据框,其中包含许多属性列和一个包含所有实际值的列,例如
PropA ... PropZ Value
green Saturn 400
green Venus 3
red Venus 2
.
.
Run Code Online (Sandbox Code Playgroud)
数据本身通过保持整洁而得到很好的处理。但有时我必须对某些属性执行一些操作(例如,比较蜜蜂红色和绿色蜜蜂(对于与其他属性相似的所有项目)可能很有趣)。因此,直接的方法是尽可能保持整洁,只弄乱我感兴趣的某些属性(PropA)。随后,我可以使用我想要的任何函数执行逐行映射,并且我可以创建一个包含函数输出的附加 PropA-Entry。
然而,在 Python 中保持所有其他属性的整洁并不像我习惯使用 R 时那么容易。原因是,所有非关键属性都通过我找到的所有 pd 方法提交给索引。如果我想保留更多的专栏,那就一团糟。
那么你如何处理这个问题。有没有其他巧妙的方法来处理这些类型的问题?
我自己写了一个传播方法,但它非常慢。也许,你对我如何改进有一些想法。
#the idea is to group by the remaining properties, which should be left in the long format.
#then i spread the small tidy data table for any group
@staticmethod
def spread(df, propcol, valcol):
def flip(data,pc,vc):
data = data.reset_index(drop=True)
return {data[pc][i]:[data[vc][i]] for i in range(0,len(data))}
#index columns are all which are not …Run Code Online (Sandbox Code Playgroud) 我正在尝试spread()几个键/值对,但公共值列不会折叠。我认为这可能与先前的处理有关,或更可能是我不知道散布两个或更多键/值对以获得我期望的结果的正确方法。
我从这个数据集开始:
library(tidyverse)
df <- tibble(order = 1:7,
line_1 = c(23,8,21,45,68,31,24),
line_2 = c(63,25,25,24,48,24,63),
line_3 = c(62,12,10,56,67,25,35))
Run Code Online (Sandbox Code Playgroud)
有2个预扩展步骤,用于定义在以下collect()函数中创建的“计数”值的顺序。这是使用行号定义“ count”变量的原始顺序的第一个预扩展步骤:
ntrl <- df %>%
gather(line_1,
line_2,
line_3,
key = "sector",
value = "count") %>%
group_by(order) %>%
mutate(sector_ord = row_number()) %>%
arrange(order,
sector)
Run Code Online (Sandbox Code Playgroud)
这是第二个预扩展步骤,用于定义“ count”变量的数字顺序:
ord <- ntrl %>%
arrange(order,
count) %>%
group_by(order) %>%
mutate(num_ord = paste0("ord_",
row_number(),
sep=""))
Run Code Online (Sandbox Code Playgroud)
最后是我一直在使用的传播代码:
wide <- ord %>%
group_by(order) %>%
spread(key = sector,
value = count) %>%
spread(key = num_ord,
value = sector_ord)
Run Code Online (Sandbox Code Playgroud)
我得到的是:
order line_1 …Run Code Online (Sandbox Code Playgroud) 如果您想尝试这些新功能(pivot_wide和pivot long),您需要安装 的开发版本tidyr:
devtools::install_github("tidyverse/tidyr")。
但我还没有实现它。我安装了一系列库,除了一个 ( vctrs) 之外,我不知道这是否是问题所在。
当我运行下一个代码时:
mtcars_wide1 <- mtcars %>%
pivot_wide(names_from = "am",
values_from = "mpg")
Run Code Online (Sandbox Code Playgroud)
R 找不到该函数。

你能给我推荐一些东西吗?
我想知道如何在传播后继续订购。
data<-tibble(var=c("A","C","D","B"), score=c(1,2,4,3))
data_spread <-data%>%spread(key = var, value = score)
Run Code Online (Sandbox Code Playgroud)
我想保持 c("A","C","D","B") 的顺序。
我有一个数组中的对象状态(在我的 ReduxReducer 中)。
const initialState = {
items: [
{ id: 1, dish: "General Chicken", price: 12.1, quantity: 0 },
{ id: 2, dish: "Chicken & Broccoli", price: 10.76, quantity: 0 },
{ id: 3, dish: "Mandaran Combination", price: 15.25, quantity: 0 },
{ id: 4, dish: "Szechuan Chicken", price: 9.5, quantity: 0 }
],
addedItems: [],
total: 0
};
Run Code Online (Sandbox Code Playgroud)
我有一个操作,当在 Cart.jsx 中单击按钮时,将对象的数量加 1,例如 {id:1,菜:Generals Chicken,价格:10.76,数量:0}。这是我尝试使用展开运算符的第一个Reducer:
case "ADD_QUANTITY":
let existing_item = state.addedItems.find(
item => action.payload === item.id
);
return { …Run Code Online (Sandbox Code Playgroud) 假设我有这些数据
4 1
4 0
4 1
3 0
5 1
Run Code Online (Sandbox Code Playgroud)
SUM如果右侧的值为1,或者为true,我如何编写一个函数(使用或类似的东西)来添加左边的所有值
总数应为13
我有一个看起来像这样的数据框(见链接).我想获取下面产生的输出,并通过在n和平均变量上扩展色调变量更进一步.似乎这个主题可能会对此产生影响,但我无法使其工作: 是否可以在tidyr中的多个列上使用传播类似于dcast?
我希望最终表将源变量放在一列中,然后将tone-n和tone-avg变量放在列中.所以我希望列标题为"source" - "For - n" - "Against - n""For -Avg" - "Against - Avg".这是出版物,不是为了进一步计算,所以它是关于呈现数据.以这种方式呈现数据对我来说似乎更直观.谢谢.
#variable1
Politician.For<-sample(seq(0,4,1),50, replace=TRUE)
#variable2
Politician.Against<-sample(seq(0,4,1),50, replace=TRUE)
#Variable3
Activist.For<-sample(seq(0,4,1),50,replace=TRUE)
#variable4
Activist.Against<-sample(seq(0,4,1),50,replace=TRUE)
#dataframe
df<-data.frame(Politician.For, Politician.Against, Activist.For,Activist.Against)
#tidyr
df %>%
#Gather all columns
gather(df) %>%
#separate by the period character
#(default separation character is non-alpha numeric characterr)
separate(col=df, into=c('source', 'tone')) %>%
#group by both source and tone
group_by(source,tone) %>%
#summarise to create counts and average
summarise(n=sum(value), avg=mean(value)) %>%
#try to spread
spread(tone, c('n', 'value'))
Run Code Online (Sandbox Code Playgroud) 我正在为网络元分析准备数据,而且我很难对列进行分类.
如果我有这个初始数据集:
Study Trt y sd n
1 1 -1.22 3.70 54
1 3 -1.53 4.28 95
2 1 -0.30 4.40 76
2 2 -2.60 4.30 71
2 4 -1.2 4.3 81
Run Code Online (Sandbox Code Playgroud)
我怎么能完成另一个呢?
Study Treatment1 y1 sd1 n1 Treatment2 y2 sd2 n2 Treatment3 y3 sd3 n3
1 1 1 -1.22 3.70 54 3 -1.53 4.28 95 NA NA NA NA
2 3 1 -0.30 4.40 76 2 -2.60 4.30 71 4 -1.2 4.3 81
Run Code Online (Sandbox Code Playgroud)
我真的陷入了这一步,我真的很感激一些帮助......
spread ×10
r ×7
tidyr ×3
dataframe ×2
dplyr ×2
dictionary ×1
excel ×1
javascript ×1
numbers ×1
pandas ×1
pivot ×1
python ×1
react-redux ×1
reactjs ×1
tidyverse ×1