这是一个示例数据框:
df <- data.frame(t1 = c(1,2,3,4), t2 = c(7,3,8,1), t3 = c(1,1,1,1))
df
t1 t2 t3
1 1 7 1
2 2 3 1
3 3 8 1
4 4 1 1
Run Code Online (Sandbox Code Playgroud)
我的目标是从每列中删除最大值.但是,对于像t3这样的列,列中的所有值都相等,我只需删除一个值,以便数据框中的所有三列最终都有三行,如下所示:
df2
t1 t2 t3
1 1 7 1
2 2 3 1
3 3 1 1
Run Code Online (Sandbox Code Playgroud) 与大多数位置查询工作相反,我实际上尝试通过其地址或 Google API 使用 Google API 来识别企业名称placeid。例如,当我在 googlemaps.com 上搜索时 1625 Wilshire Blvd, Los Angeles, CA 90017,其结果显示“在此位置”是“麦当劳”。
但是,当使用以下 URL 进行 API 调用时,名称会显示为街道地址,而我想要的是识别该位置的企业名称(“麦当劳”):
\n\nhttps://maps.googleapis.com/maps/api/place/textsearch/json?query=1625%20Wilshire%20Blvd,%20Los%20Angeles,%20CA%2090017&sensor=false&key=<api_key>
编辑:使用@xomena 推荐的确切调用,我仍然遇到同样的问题。我已经用 R 和 Python 运行了它,并且我\xe2\x80\x99m 得到的结果是街道地址,而不是两种方式的名称。
\n\nR代码:
\n\npackages <- c("RJSONIO")\nnew_packages <- packages[!(packages %in% installed.packages()[,"Package"])]\nif(length(new_packages)) install.packages(new_packages)\n\nlibrary(RJSONIO)\nRun Code Online (Sandbox Code Playgroud)\n\nfromJSON(URLencode(paste("https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=1625%20Wilshire%20Blvd%2C%20Los%20Angeles%2C%20CA%2090017&inputtype=textquery&fields=formatted_address,name,place_id&key=", api_key, sep = "")))
R输出:
\n\n$candidates\n$candidates[[1]]\n formatted_address \n"1625 Wilshire Blvd, Los Angeles, CA 90017, USA" \n name \n "1625 Wilshire Blvd" \n place_id \n "ChIJ18AW_aPHwoARXRm-cgcRcDs" \n\n\n$debug_log\n$debug_log$line\nlist()\n\n\n$status\n[1] "OK"\n …Run Code Online (Sandbox Code Playgroud) 从以下data.table开始:
set.seed(1234)
dt <- data.table(x = runif(3), y = runif(3), z = runif(3))
print(dt)
# x y z
#1: 0.1137034 0.6233794 0.009495756
#2: 0.6222994 0.8609154 0.232550506
#3: 0.6092747 0.6403106 0.666083758
Run Code Online (Sandbox Code Playgroud)
并将其转换为以下结构中的列表:
print(dt2)
#[[1]]
#[1] 0.1137034 0.6233794 0.009495756
#
#[[2]]
#[1] 0.6222994 0.8609154 0.2325505
#
#[[3]]
#[1] 0.6092747 0.6403106 0.6660838
Run Code Online (Sandbox Code Playgroud)
我一直在研究这个问题的答案,但是没有能够在不应用循环函数的情况下立即弄清楚如何对data.table的所有行执行此操作.我正在尝试避免循环函数,因为实际data.table中的行数.
我有以下数据集:
wow <- data.frame(a = c(1, 1, 1, 2, 3, 4, 4), b = c(3, 4, 2, 6, 2, 6, 5), c = c(1, 6, 3, 6, 1, 8, 9))
print(wow)
a b c
1 1 3 1
2 1 4 6
3 1 2 3
4 2 6 6
5 3 2 1
6 4 6 8
7 4 5 9
Run Code Online (Sandbox Code Playgroud)
我需要从每列中删除所有最小值和最大值,然后计算剩余值的平均值,以便结果如下所示:
print(result)
a b c
1 2.5 4 5.75
Run Code Online (Sandbox Code Playgroud)
我发现了一个已经回答的类似问题(平均值来自数据框中的行值,不包括 R 中的最小值和最大值),但最大的区别是提出该问题的人仅处理每列中的单个最小值和最大值,而我可以在一列中有多个最小值和最大值。
我有以下数据框:
df <- data.frame(type = c("planes", "trains", "automobiles"), t1 = c(4, 5, 6), t2 = c(20, 60, 24), t3 = c(100, 120, 72), t4 = c(800, 360, 144))
df
type t1 t2 t3 t4
1 planes 4 20 100 800
2 trains 5 60 120 360
3 automobiles 6 24 72 144
Run Code Online (Sandbox Code Playgroud)
我想编写一个函数,它接受每列中的值并将它们除以前一列,从t2/t1开始,这样我得到一个如下所示的新数据框:
new_df
type t1 t2 t3 t4
1 planes 5 5 8
2 trains 12 2 3
3 automobiles 4 3 2
Run Code Online (Sandbox Code Playgroud)
使用扫描功能可能有一种方法可以做到这一点,但我还没有找到它.
提前致谢!
我有以下 data.table,其中每个唯一x值都与一个唯一y值相关联。然后我强制一个x值作为NAk 最近邻练习的目的:
dt <- data.table(x = rep(c(1:4), 3),
y = rep(c("Brandon", "Erica", "Karyna", "Alex"), 3))
dt[3, 1] <- NA
print(dt)
# x y
#1: 1 Brandon
#2: 2 Erica
#3: NA Karyna
#4: 4 Alex
#5: 1 Brandon
#6: 2 Erica
#7: 3 Karyna
#8: 4 Alex
#9: 1 Brandon
#10: 2 Erica
#11: 3 Karyna
#12: 4 Alex
Run Code Online (Sandbox Code Playgroud)
参考这个问题的第一个答案,我创建了一个二进制矩阵,dt$y如下所示:
dt.a <- model.matrix(~ y -1 , data …Run Code Online (Sandbox Code Playgroud) 我有以下内容data.table:
dt1 <- data.table(apple = c(1:5),
bananas = c(5:1),
carrots = c(6:10),
donuts = c(11:15))
Run Code Online (Sandbox Code Playgroud)
以下内容list:
names_to_keep <- c("apple", "carrots")
Run Code Online (Sandbox Code Playgroud)
我需要创建一个新data.table的dt1,只包括包含其名称的列names_to_keep.
期望的结果:
# apple carrots
#1: 1 6
#2: 2 7
#3: 3 8
#4: 4 9
#5: 5 10
Run Code Online (Sandbox Code Playgroud)