小编Ign*_*cio的帖子

使用dplyr创建排名变量?

假设我有以下数据

df = data.frame(name=c("A", "B", "C", "D"), score = c(10, 10, 9, 8))
Run Code Online (Sandbox Code Playgroud)

我想在排名中添加一个新列.这就是我正在做的事情:

df %>% mutate(ranking = rank(score, ties.method = 'first'))
#   name score ranking
# 1    A    10       3
# 2    B    10       4
# 3    C     9       2
# 4    D     8       1
Run Code Online (Sandbox Code Playgroud)

但是,我想要的结果是:

#   name score ranking
# 1    A    10       1
# 2    B    10       1
# 3    C     9       2
# 4    D     8       3
Run Code Online (Sandbox Code Playgroud)

显然rank没有做我的想法.我应该使用什么功能?

r dplyr

41
推荐指数
2
解决办法
3万
查看次数

在R中将日期转换为unix时间

我想将"2013-09-16"转换为unix时间.

我发现在R中将Convert UNIX纪元转换为Date对象,但我需要反过来做.

谢谢!

r

29
推荐指数
2
解决办法
3万
查看次数

当我在`dplyr`之后加载`plyr`时,为什么汇总或变异不能用于group_by?

注意:此问题的标题已经过编辑,使其成为plyr功能掩盖其dplyr对应项时的问题的规范问题.问题的其余部分保持不变.


假设我有以下数据:

dfx <- data.frame(
  group = c(rep('A', 8), rep('B', 15), rep('C', 6)),
  sex = sample(c("M", "F"), size = 29, replace = TRUE),
  age = runif(n = 29, min = 18, max = 54)
)
Run Code Online (Sandbox Code Playgroud)

有了旧的,plyr我可以使用以下代码创建一个总结我的数据的小表:

require(plyr)
ddply(dfx, .(group, sex), summarize,
      mean = round(mean(age), 2),
      sd = round(sd(age), 2))
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样:

  group sex  mean    sd
1     A   F 49.68  5.68
2     A   M 32.21  6.27
3     B   F 31.87  9.80
4     B   M 37.54 …
Run Code Online (Sandbox Code Playgroud)

r plyr r-faq dplyr

17
推荐指数
1
解决办法
8669
查看次数

在R中用传单悬停时弹出?

我的传单地图看起来像这样:

library(sp)
library(leaflet)
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  Sr1 = Polygon(cbind(xx, yy))
  Srs1 = Polygons(list(Sr1), "s1")
  SpP = SpatialPolygons(list(Srs1), 1:1)
  return(SpP)
}
Circle.Town <- circleFun(c(1,-1),2.3,npoints = 100)

df1 <- data.frame(long=c(0.6,1,1.4), lat=c(-2, -.8, -0.2), other=c('a', 'b', 'c'), VAM=c(10,8,6), 
                  type=c('Public', 'Public', 'Private'), id=c(1:3)) %>% 
  mutate(X=paste0('<strong>id: </strong>', 
                  id,
                  '<br><strong>type</strong>: ',
                  type,
                  '<br><strong>VAM</strong>: ',
                  VAM)) …
Run Code Online (Sandbox Code Playgroud)

r leaflet r-leaflet

17
推荐指数
1
解决办法
7524
查看次数

tidyR的长数据到宽数据?

我的数据看起来像这样

df = data.frame(name=c("A","A","B","B"),
                group=c("g1","g2","g1","g2"),
                V1=c(10,40,20,30),
                V2=c(6,3,1,7))
Run Code Online (Sandbox Code Playgroud)

我想重塑它看起来像这样:

df = data.frame(name=c("A", "B"),               
                V1.g1=c(10,20),
                V1.g2=c(40,30),
                V2.g1=c(6,1),
                V2.g2=c(3,7))
Run Code Online (Sandbox Code Playgroud)

用tidyR可以做到吗?

我可以用重塑来做到这一点

reshape(df, idvar='name', timevar='group', direction='wide')
Run Code Online (Sandbox Code Playgroud)

但是学习新东西总是好的.

r tidyr

13
推荐指数
3
解决办法
5197
查看次数

尚未支持mclapply长向量

我正在尝试运行一些R代码,因为内存而崩溃.我得到的错误是:

Error in sendMaster(try(lapply(X = S, FUN = FUN, ...), silent = TRUE)) : 
  long vectors not supported yet: memory.c:3100
Run Code Online (Sandbox Code Playgroud)

产生麻烦的功能如下:

StationUserX <- function(userNDX){
  lat1 = deg2rad(geolocation$latitude[userNDX])
  long1 = deg2rad(geolocation$longitude[userNDX])
  session_user_id = as.character(geolocation$session_user_id[userNDX])
  #Find closest station
  Distance2Stations <- unlist(lapply(stationNDXs, Distance2StationX, lat1, long1))
  # Return index for closest station and distance to closest station
  stations_userX = data.frame(session_user_id = session_user_id, 
                              station = ghcndstations$ID[stationNDXs], 
                              Distance2Station = Distance2Stations)    
  stations_userX = stations_userX[with(stations_userX, order(Distance2Station)), ]
  stations_userX = stations_userX[1:100,] #only the 100 closest stations...
  row.names(stations_userX)<-NULL
  return(stations_userX)
} …
Run Code Online (Sandbox Code Playgroud)

r

11
推荐指数
1
解决办法
2894
查看次数

使用stargazer添加一行注释

我想运行一些回归并用stargazer创建一个表.例如

linear.1 <- lm(rating ~ complaints + privileges + learning + raises + critical, data=attitude)
linear.2 <- lm(rating ~ complaints + privileges + learning, data=attitude)
stargazer(linear.1, linear.2, type="text", keep.stat=c("n"))
Run Code Online (Sandbox Code Playgroud)

对于某些规格,我想在表中添加几行"X".那是:

                                       rating                    
                             (1)                    (2)          
-----------------------------------------------------------------
complaints                 0.692***               0.682***       
                           (0.149)                (0.129)        

privileges                  -0.104                 -0.103        
                           (0.135)                (0.129)        

learning                    0.249                  0.238*        
                           (0.160)                (0.139)        

raises                      -0.033                               
                           (0.202)                               

critical                    0.015                                
                           (0.147)                               

Constant                    11.011                 11.258        
                           (11.704)               (7.318)        

Note 1                          X                                
Note 2                                                 X        
-----------------------------------------------------------------
Observations                  30                     30          
=================================================================
Note:                                 *p<0.1; **p<0.05; ***p<0.01 
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

谢谢!

r stargazer

11
推荐指数
1
解决办法
3930
查看次数

与lubridate的年份时差?

我想用它lubridate来计算出生日期和今天的日期.现在我有这个:

library(lubridate)
today<-mdy(08312015)
dob<-mdy(09071982)
today-dob
Run Code Online (Sandbox Code Playgroud)

这给了我他们几天的年龄.

r lubridate

11
推荐指数
3
解决办法
1万
查看次数

如何在ggplot2中的x轴下添加注释?

我有以下图表:

library(ggplot2)
library(scales)
library(magrittr)
df1 <-
  structure(
    list(
      x = structure(
        1:5, .Label = c("5", "4", "3", "2",
                        "1"), class = "factor"
      ), y = c(
        0.166666666666667, 0.361111111111111,
        0.0833333333333333, 0.222222222222222, 0.291666666666667
      )
    ), .Names = c("x",
                  "y"), row.names = c(NA,-5L), class = c("tbl_df", "tbl", "data.frame"), drop = TRUE
  )

df1 %>% ggplot(aes(x , y )) + geom_bar(stat = "identity") +
  scale_y_continuous(labels = percent) 
Run Code Online (Sandbox Code Playgroud)

我想在5和1之下添加带有粗体文本的两行注释.例如,'最高\nvalue'低于5,'最低\n值'低于1.

我试过geom_text但我不能把文字放在我想要的地方.

r ggplot2

10
推荐指数
2
解决办法
9983
查看次数

创建一个空列表,用R中的列表填充它

我想创建一个空列表,以便我可以用其他列表替换它的元素.

例如

simulations = 10
seeds = sample(10000:99999, simulations, replace=F)

test_function <- function(seedX){
  lista = list(seedX=seedX, 
               dataframe=data.frame(x=runif(10, -5.0, 5.0),y=rnorm(10,mean = 0,sd = 1)))
  return(lista)
}
results <- vector("list", simulations) 
results[1] = test_function(seedX = seeds[1])
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Warning message:
In results[1] = test_function(seedX = seeds[1]) :
  number of items to replace is not a multiple of replacement length
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

谢谢!

r

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

r ×10

dplyr ×2

ggplot2 ×1

leaflet ×1

lubridate ×1

plyr ×1

r-faq ×1

r-leaflet ×1

stargazer ×1

tidyr ×1