我正在尝试从长到宽重塑数据集。以下代码有效,但我很好奇是否有办法不提供值列并仍然使用pivot_wider. 在下面的示例中,我必须创建一个临时列“val”才能使用pivot_wider,但是有没有办法不用它呢?
a <- data.frame(name = c("sam", "rob", "tom"),
type = c("a", "b", "c"))
a
name type
1 sam a
2 rob b
3 tom c
Run Code Online (Sandbox Code Playgroud)
我想将其转换如下。
name a b c
1 sam 1 0 0
2 rob 0 1 0
3 tom 0 0 1
Run Code Online (Sandbox Code Playgroud)
这可以通过以下代码完成,但我可以在不创建“val”列的情况下完成(并且仍然使用 tidyverse 语言)吗?
a <- data.frame(name = c("sam", "rob", "tom"),
type = c("a", "b", "c"),
val = rep(1, 3)) %>%
pivot_wider(names_from = type, values_from = val, values_fill = list(val = …Run Code Online (Sandbox Code Playgroud) 我正在尝试sf通过应用st_simplify. CRS 是 4267 并尝试使用正确的dTolerance. 我知道单位dTolerance必须是 CRS 的单位,所以我从 0.1 开始,但我不断收到此错误消息。
test <- st_read("comm_sf.shp") %>%
+ st_simplify(preserveTopology = T,
+ dTolerance = 0.1)
Simple feature collection with 11321 features and 21 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -124.4375 ymin: 24.5441 xmax: -66.94983 ymax: 49.00249
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
Warning message:
In st_simplify.sfc(st_geometry(x), preserveTopology, dTolerance) :
st_simplify does not correctly simplify longitude/latitude data, dTolerance needs to be in decimal …Run Code Online (Sandbox Code Playgroud) 我正在尝试按组折叠/聚合/汇总行,仅保留非缺失值,其中值是字符。这是一个可重现的示例。
df = data.frame(store = c("A","A", "B","B"),
item1=c("apple","","milk",""),
item2=c("","pear","","bread"))
df
store item1 item2
1 A apple
2 A pear
3 B milk
4 B bread
Run Code Online (Sandbox Code Playgroud)
我希望将 df 更改为以下内容
df2
store item1 item2
1 A apple pear
2 B milk bread
Run Code Online (Sandbox Code Playgroud)
我尝试过使用summarise_allwith ,nchar(.) > 0如下所示,但它似乎不起作用。
df %>%
group_by(store) %>%
summarise_all( ~ + any(nchar(.) > 0))
Run Code Online (Sandbox Code Playgroud)
任何意见将不胜感激!
我正在尝试向 Y 轴上的水平线添加注释。在查看了类似的问题后,我创建了一些类似的东西,但并不完全是我想要的。具体来说,我希望将文本“高”放置在 Y 轴(图外)低于 6(在 Y 轴上)。这是我迄今为止尝试过的。
set.seed(57)
discharge <- data.frame(date = seq(as.Date("2011-01-01"), as.Date("2011-12-31"), by="days"),
discharge = rexp(365))
ggplot(discharge) +
geom_line(aes(x = date, y = discharge)) +
geom_hline(yintercept = 5.5, linetype= "dashed", color = "red") +
geom_text(aes(x = date[13], y = 5.5, label = "High"))
Run Code Online (Sandbox Code Playgroud)
任何建议,将不胜感激!
我正在尝试将数据从长调整为宽,但在这里我需要创建名称列,例如 event1、event2、event3 等。换句话说,没有自然的参数候选者names_from。我尝试了几种不同的方法,但无法得到我正在寻找的东西 - 这是一个可重现的例子。
set.seed(57)
df <- data.frame(date = seq.Date(as.Date("2009-01-01"), as.Date("2009-01-12"), by = 1),
id = rep(1:3, each = 4),
val = rnorm(12)) %>% filter(val > 0.5)
Run Code Online (Sandbox Code Playgroud)
我想转换df为df2.
df2 <- data.frame(id = c(1:3),
event1 = c("2009-01-03", "2009-01-06", "2009-01-10"),
event2 = c("2009-01-04", "2009-01-07", "2009-01-11"),
event3 = c(" ", " ", "2009-01-12"))
Run Code Online (Sandbox Code Playgroud)
请注意,此数据集记录了每个 ID 的第一次、第二次和第三次(如果有)出现日期。
这是我尝试使用names_prefix但似乎不起作用的方法。
set.seed(57)
df <- data.frame(date = seq.Date(as.Date("2009-01-01"), as.Date("2009-01-12"), by = 1),
id = rep(1:3, each = 4),
val …Run Code Online (Sandbox Code Playgroud) 这是一个与此类似的问题(R Mutate multiple columns with ifelse()-condition),但我无法将其应用于我的问题。
这是一个可重现的示例:
df <- structure(list(comm_id = c("060015", "060015", "060015", "060015",
"060015", "060015", "060015", "060015", "060015", "060015", "060015"
), trans_year = c(1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
2000, 2001, 2002), f10_1 = c(1996, 1996, 1996, 1996, 1996, 1996,
1996, 1996, 1996, 1996, 1996), f10_2 = c(1997, 1997, 1997, 1997,
1997, 1997, 1997, 1997, 1997, 1997, 1997)), row.names = c(NA,
-11L), class = c("tbl_df", "tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
我想使用条件创建额外的列(在我的实际问题中,以类似的方式超过 10 列)ifelse,这可以用蛮力完成如下。但是我的实际问题有 10 多个这样的列,所以它会从更优雅的方法中受益很多。 …