我需要在分析中按天重命名数据框。
names(dados) <- c("name", "day_1","Freq_1","Percent_1","day_2","Freq_2","Percent_2",
"day_3","Freq_3","Percent_3","day_4","Freq_4","Percent_4",
"day_5","Freq_5","Percent_5","day_6","Freq_6","Percent_6",
"day_7","Freq_7","Percent_7","day_8","Freq_8","Percent_8",
"day_9","Freq_9","Percent_9")
我正在分析我得到的数据是在数据框列表中,每个数据框代表一天的分析。我合并了数据框,我有列&#39; name&#39;独特的&#39; day_X&#39;,&#39; Freq_X&#39;和&#39; Percent_X&#39;将每个数据帧作为回报。
作为返回,我需要列具有以下名称:
&#34; name&#34;,&#34; day_1&#34;,&#34; Freq_1&#34;,&#34; Percent_1&#34;,&#34; day_2&#34;,&# 34; Freq_2&#34;&#34; Percent_2&#34;&#34; day_3&#34;&#34; Freq_3&#34;&#34; Percent_3&#34;
如何分析50天?
可重复的例子:
day1 <- data.frame(name = c("jose", "mary", "julia"), freq = c(1,5,3), percent = c(40,30,20))
day2 <- data.frame(name = c("abner", "jose", "mary"), freq = c(3,5,4), percent = c(20,30,20))
day3 <- data.frame(name = c("abner", "jose", "mike"), freq = c(6,2,3), percent = c(40,30,70))
day4 <- data.frame(name = c("andre", "joseph", "ana"), freq = c(1,5,8), percent = c(40,30,20))
day5 <- data.frame(name = c("abner", "poli", "joseph"), freq = c(4,3,3), percent = c(10,30,10))
dates <- list(day1,day2,day4,day5)
data <- Reduce(function(x, y) merge(x, y, by = "name", all = TRUE), dates)
答案 0 :(得分:1)
这是使用Get-ChildItem
套件套件获得所需内容的方法。我们首先将数据放在“长”格式中 - 但添加一个日期为:
tidyverse
现在,为了获得您所需的宽格式,我们需要重新格式化一些内容,如下面的代码所示。我不确定long_form <- dates %>%
imap_dfr(function(x, y) dplyr::mutate(x, day_num = y))
变量中应该包含哪些内容,如评论中提到的@useR,所以它不见了。如果你有一个名为day_#
的变量,代码应该自动做正确的事情。
day
答案 1 :(得分:0)
可以使用dplyr::bind_rows
将列表中的所有数据帧合并到数据帧。请提供name
列表,以便事先设置day1
,day2
等。最后,gather
和spread
用于转换数据。
names(dates) <- paste("day", seq_along(dates), sep = "")
library(tidyverse)
bind_rows(dates,.id = "Name") %>%
group_by(Name) %>%
mutate(rn = row_number()) %>%
ungroup() %>%
gather(Key, value, -Name,-rn) %>%
unite("Key", c("Key", "Name")) %>%
spread(Key, value) %>%
select(-rn)
<强>结果:强>
# # A tibble: 3 x 12
# freq_day1 freq_day2 freq_day3 freq_day4 name_day1 name_day2 name_day3 name_day4 percent_day1 percent_day2 percent~ percent~
# * <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
# 1 1 3 1 4 jose abner andre abner 40 20 40 10
# 2 5 5 5 3 mary jose joseph poli 30 30 30 30
# 3 3 4 8 3 julia mary ana joseph 20 20 20 10
#
数据:强>
从OP略微修改数据。我已将stringsAsFactors = FALSE
参数作为data.frame
的一部分,以避免mutate_at
调用将factor
转换为character
。
day1 <- data.frame(name = c("jose", "mary", "julia"), freq = c(1,5,3), percent = c(40,30,20), stringsAsFactors = FALSE)
day2 <- data.frame(name = c("abner", "jose", "mary"), freq = c(3,5,4), percent = c(20,30,20), stringsAsFactors = FALSE)
day3 <- data.frame(name = c("abner", "jose", "mike"), freq = c(6,2,3), percent = c(40,30,70), stringsAsFactors = FALSE)
day4 <- data.frame(name = c("andre", "joseph", "ana"), freq = c(1,5,8), percent = c(40,30,20), stringsAsFactors = FALSE)
day5 <- data.frame(name = c("abner", "poli", "joseph"), freq = c(4,3,3), percent = c(10,30,10), stringsAsFactors = FALSE)
dates <- list(day1,day2,day4,day5)