我想要添加一个列,data.table它是几个其他列的串联,其中的名称我存储在向量中cols.根据/sf/answers/1517778181/我试过do.call+ paste但无法使其正常工作.这是我尝试过的:
# Using mtcars as example, e.g. first record should be "110 21 6"
dt <- data.table(mtcars)
cols <- c("hp", "mpg", "cyl")
# Works old-fashioned way
dt[, slice.verify := paste(hp, mpg, cyl)]
# Raw do.call+paste fails with message:
# Error in do.call(paste, cols): second argument must be a list
dt[, slice := do.call(paste, cols)]
# Making cols a list makes the column "hpmpgcyl" for each row
dt[, slice := do.call(paste, …Run Code Online (Sandbox Code Playgroud) 我想在data.table对象中添加一列,列出该行的NA列名.例如,假设我有以下data.table:
dt <- data.table(a = c(1, 2, 3, NA),
b = c(1, 2, NA, NA),
c = c(NA, 2, NA, 4))
a b c
1: 1 1 NA
2: 2 2 2
3: 3 NA NA
4: NA NA 4
Run Code Online (Sandbox Code Playgroud)
我想添加一个包含这些值的列,产生以下data.table:
dt[, na.cols := c("c", "", "b,c", "a,b")]
a b c na.cols
1: 1 1 NA c
2: 2 2 2
3: 3 NA NA b,c
4: NA NA 4 a,b
Run Code Online (Sandbox Code Playgroud)
如何动态添加此列?
我想用多个序列在ggplot上标记一条水平线,而不必将该线与一系列相关联。R ggplot2:用数字值标记y轴上的水平线会询问单序列情况,该geom_text问题可以解决。但是,geom_text通过颜色和图例将标签与系列之一相关联。
考虑来自该问题的相同示例,另一color列:
library(ggplot2)
df <- data.frame(y=1:10, x=1:10, col=c("a", "b")) # Added col
h <- 7.1
plot1 <- ggplot(df, aes(x=x, y=y, color=col)) + geom_point()
plot2 <- plot1 + geom_hline(aes(yintercept=h))
# Applying top answer /sf/answers/901362171/
plot2 + geom_text(aes(0, h, label=h, vjust=-1))
Run Code Online (Sandbox Code Playgroud)
如何在不将标签关联到系列之一的情况下标记行?
我想根据 Google 表格中的名称和地址检索地点的位置,例如:
=PlaceAddress("White House, Washington, DC")
=PlaceAddress("Moma, New York, NY")
=PlaceAddress("Google, Mountain View, CA")
Run Code Online (Sandbox Code Playgroud)
这将返回结果(分别)出现在 Google 搜索中,即:
1600 宾夕法尼亚大道 NW, 华盛顿特区 20500
11 W 53rd St, 纽约, NY 10019
1600 Amphitheatre Pkwy, 山景城, CA 94043
Maps Apps Script 服务有一个地理编码器,但这似乎需要地址,而不是地名。我猜该解决方案涉及我在 Apps Script 中遗漏的某些功能,或者类似于ImportData从 Google 搜索或其他服务获取结构化数据的功能。
k当分箱到等宽箱中时,我想将数字向量的每个元素与其箱的中点相关联。
ggplot2::cut_interval可以产生等宽的垃圾箱,并且Hmisc::cut2可以提供中点,但我没有找到同时完成这两者的方法。
最小可重现示例:
v <- c(1, 2, 7, 9)
# cut_interval gives equal-width bins, but no midpoints.
ggplot2::cut_interval(v, 2)
# [1] [1,5] [1,5] (5,9] (5,9]
# Levels: [1,5] (5,9]
# cut2 doesn't give equal-width bins.
Hmisc::cut2(v, g=2)
# [1] [1,7) [1,7) [7,9] [7,9]
# Levels: [1,7) [7,9]
# But it returns the midpoint.
Hmisc::cut2(v, g=2, levels.mean=T)
# [1] 1.5 1.5 8.0 8.0
# Levels: 1.5 8.0
# Which can be extracted as a numeric.
as.numeric(as.character(Hmisc::cut2(v, g=2, …Run Code Online (Sandbox Code Playgroud) 我想向 a 添加一个向量data.table,其中向量元素的每个名称对应于一个新列,并且该值根据向量的元素值重复。
所以给出:
x <- data.table(a=1:2, b=3:4)
v <- c(c=5, d=6)
Run Code Online (Sandbox Code Playgroud)
我正在寻求以下结果:
data.table(a=1:2, b=3:4, c=5, d=6)
# a b c d
# 1: 1 3 5 6
# 2: 2 4 5 6
Run Code Online (Sandbox Code Playgroud)
我尝试过的事情:
cbind 将向量添加为单列:
cbind(x, v)
# a b v
# 1: 1 3 5
# 2: 2 4 6
Run Code Online (Sandbox Code Playgroud)
使用将多列添加到 data.table 中的方法,其中列名保存在向量中也会错误地矢量化(水平而不是垂直):
x[, (names(v)) := v]
# a b c d
# 1: 1 3 5 5
# 2: 2 …Run Code Online (Sandbox Code Playgroud) 给定任意数量的列表,我想生成一个熊猫DataFrame作为笛卡尔积。例如,给定:
a = [1, 2, 3]
b = ['val1', 'val2']
c = [100, 101]
Run Code Online (Sandbox Code Playgroud)
我想DataFrame以列a、b、 和c以及所有 3x2x2=12 组合结束。
与pandas 中的笛卡尔积不同,我正在寻找提供两个以上输入的能力,并且我不希望传递DataFrames,这将涉及将值保持在相同的范围内DataFrame而不是将其组合。这个问题的答案可能不会与那个问题的答案重叠。
与x 和 y 数组点的笛卡尔积点成 2D 点的单个数组不同,我正在寻找DataFrame带有命名列的 Pandas结果,而不是二维 numpy 数组。
如何删除播放和停止按钮并只保留滑块?
import plotly.express as px
import pandas as pd
df = pd.DataFrame(dict(x=[0, 1, 0, 1],
y=[0, 1, 1, 0],
z=[0, 0, 1, 1]))
px.line(df, "x", "y", animation_frame="z")
Run Code Online (Sandbox Code Playgroud)
我有两个data.table:
k要从中提取顶部的值,每个group.group到要k为其选择的值的映射group。如何在 R 数据框中按组或在类别内(按组)查找前 N 个值解决了这个问题,当k不因组而异时。我怎样才能做到这一点?这是示例数据和所需的结果:
价值观:
(dt <- data.table(id=1:10,
group=c(rep(1, 5), rep(2, 5))))
# id group
# 1: 1 1
# 2: 2 1
# 3: 3 1
# 4: 4 1
# 5: 5 1
# 6: 6 2
# 7: 7 2
# 8: 8 2
# 9: 9 2
# 10: 10 2
Run Code Online (Sandbox Code Playgroud)
映射group到k:
(group.k <- …Run Code Online (Sandbox Code Playgroud) Stata.dta文件包括每列的标签/描述,可以使用describe命令在 Stata 中查看。例如,这个在线数据集中的adults和kids变量,分别有描述和:number of adults in householdnumber of children in household
clear
use http://www.principlesofeconometrics.com/stata/alcohol.dta
describe
Contains data from http://www.principlesofeconometrics.com/stata/alcohol.dta
obs: 1,000
vars: 4 10 Nov 2007 11:33
size: 5,000 (_dta has notes)
-------------------------------------------------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------------------------------------------------------------
adults byte %8.0g number of adults in household
kids byte %8.0g number of children in household
income int %8.0g weekly income
consume byte %8.0g …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个R ggplot2图表,其中列分组一个变量,a facet_grid分解另一个变量.我试过的两种方式都失败了:
1)设置变量x按该facet_grid变量分解两次:
ggplot(diamonds, aes(x=cut, y=price, fill=color)) +
geom_bar(position="dodge", stat="identity") +
facet_grid(cut ~ ., scales="free")
Run Code Online (Sandbox Code Playgroud)
2)删除x会产生错误:
ggplot(diamonds, aes(y=price, fill=color)) +
geom_bar(position="dodge", stat="identity") +
facet_grid(cut ~ ., scales="free")
# Error in exists(name, envir = env, mode = mode):
# argument "env" is missing, with no default
Run Code Online (Sandbox Code Playgroud) 我想计算DataFrame一次只发生一次的行.例如,在下面的示例中,我返回2,因为只有行索引2和3出现一次:
In [1]: df = pd.DataFrame({'a': [1, 1, 2, 3], 'b': [1, 1, 2, 2]})
In [2]: df
Out[2]:
a b
0 1 1
1 1 1
2 2 2
3 3 2
Run Code Online (Sandbox Code Playgroud)
(这与询问唯一行的数量不同,在这种情况下,每个行的数量为3 df.drop_duplicates().shape[0].