我希望通过对第二个变量进行分组来计算唯一值的数量,然后将计数添加到现有data.frame作为新列.例如,如果现有数据框如下所示:
color type
1 black chair
2 black chair
3 black sofa
4 green sofa
5 green sofa
6 red sofa
7 red plate
8 blue sofa
9 blue plate
10 blue chair
Run Code Online (Sandbox Code Playgroud)
我想为每个添加数据中存在color的唯一计数types:
color type unique_types
1 black chair 2
2 black chair 2
3 black sofa 2
4 green sofa 1
5 green sofa 1
6 red sofa 2
7 red plate 2
8 blue sofa 3
9 blue plate 3
10 blue chair …Run Code Online (Sandbox Code Playgroud) 我试图在一组固定的地理区域内创建Voronoi多边形(又名Dirichlet镶嵌或Thiessen多边形).但是,我在R中找到一个方法会遇到地图边界内的多边形.我的主要目标是获得准确的面积计算(不仅仅是生成视觉图).例如,以下内容直观地传达了我想要实现的目标:
library(maps)
library(deldir)
data(countyMapEnv)
counties <- map('county', c('maryland,carroll','maryland,frederick', 'maryland,montgomery', 'maryland,howard'), interior=FALSE)
x <- c(-77.208703, -77.456582, -77.090600, -77.035668, -77.197144)
y <- c(39.188603, 39.347019, 39.672818, 39.501898, 39.389203)
points(x,y)
vt <- deldir(x, y, rw=counties$range)
plot(vt, wlines="tess", lty="solid", add=TRUE)
Run Code Online (Sandbox Code Playgroud)
产生以下内容:

从概念上讲,我想counties与之相交vt,应该提供一组由县界限定的多边形,并为每个多边形进行准确的面积计算.现在,vt$summary为每个多边形提供面积计算,但除了一个内部多边形之外,它们显然被夸大了,并且deldir()似乎只接受其rw参数的矩形包围.我是R的geospacial能力的新手,所以我可以接受超出我上面概述的其他方法.
该stargazer软件包的5.2 版似乎存在一个错误,omit.label根据所包含模型的顺序,该功能无法始终如一地工作:
library(stargazer)
library(ggplot2)
as.data.frame(data("midwest"))
fit.1 <- lm(poptotal ~ popadults, data = midwest)
fit.2 <- lm(poptotal ~ popadults + state, data = midwest)
# Works, column listed as "Yes":
stargazer(fit.2, omit = c("state"), omit.labels = c("States"))
# Does not work, both columns listed as "No":
stargazer(fit.1, fit.2, omit = c("state"), omit.labels = c("States"))
# Works, first column "Yes", second "No":
stargazer(fit.2, fit.1, omit = c("state"), omit.labels = c("States"))
Run Code Online (Sandbox Code Playgroud)
有谁知道解决方法?
有没有办法让notes观星者换行而不是跑掉页面?
stargazer(fit.1, notes="A very very long note that I would like to put below the table, but currently runs off the side of the page when I compile my document. How do I get this to wrap into paragraph form?")
Run Code Online (Sandbox Code Playgroud)
哪个产生:
\hline \\[-1.8ex]
\textit{Notes:} & \multicolumn{2}{l}{$^{*}$P $<$ .05} \\
& \multicolumn{2}{l}{$^{**}$P $<$ .01} \\
& \multicolumn{2}{l}{$^{***}$P $<$ .001} \\
& \multicolumn{2}{l}{A very very long note that I would like to put below the table, but currently runs off …Run Code Online (Sandbox Code Playgroud) 我在理解childNodes中存储的内容时遇到了一些麻烦.理想情况下,我想在每个子节点上做另一个xquery,但似乎无法直截了当.这是我的方案:数据:
<div class="something">
<h3>
<a href="link1.html">Link text 1</a>
</h3>
<div class"somethingelse">Something else text 1</div>
</div>
<div class="something">
<h3>
<a href="link2.html">Link text 2</a>
</h3>
<div class"somethingelse">Something else text 2</div>
</div>
<div class="something">
<h3>
<a href="link3.html">Link text 3</a>
</h3>
<div class"somethingelse">Something else text 3</div>
</div>
Run Code Online (Sandbox Code Playgroud)
和代码:
$html = new DOMDocument();
$html->loadHtmlFile($local_file);
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "//div[@class='something']");
foreach ($nodelist as $n) {
Can I run another query here? }
Run Code Online (Sandbox Code Playgroud)
对于"某事"的每个元素(即$ n),我想访问两段文本和href的值.我尝试使用childNode和另一个xquery,但无法获得任何工作.任何帮助将不胜感激!
我编写了以下代码,但是一旦我开始在数千条记录上执行它,它就会非常缓慢:
require("RJSONIO")
people_data <- data.frame(person_id=numeric(0))
json_data <- fromJSON(json_file)
n_people <- length(json_data)
for(person in 1:n_people) {
person_dataframe <- as.data.frame(t(unlist(json_data[[person]])))
people_data <- merge(people_data, person_dataframe, all=TRUE)
}
output_file <- paste("people_data",".csv")
write.csv(people_data, file=output_file)
Run Code Online (Sandbox Code Playgroud)
我试图从一系列json格式的文件构建一个统一的数据表.该fromJSON()函数将数据作为列表列表读入.列表的每个元素都是一个人,然后包含该人的属性列表.
例如:
[[1]]
person_id
name
gender
hair_color
[[2]]
person_id
name
location
gender
height
[[...]]
structure(list(person_id = "Amy123", name = "Amy", gender = "F",
hair_color = "brown"),
.Names = c("person_id", "name", "gender", "hair_color"))
structure(list(person_id = "matt53", name = "Matt",
location = structure(c(47231, "IN"),
.Names = c("zip_code", "state")),
gender = "M", …Run Code Online (Sandbox Code Playgroud) 我有一个“csv”文本文件,其中每个字段都由\t&%$#我现在尝试导入到 R 中分隔。
坚持单个字符的sep=论点read.table()。有没有直接导入这个文件的快速方法?
一些数据字段是用户提交的文本,其中包含制表符、引号和其他杂乱的内容,因此将分隔符更改为更简单的内容似乎会产生其他问题。