标签: data-manipulation

不同长度向量的均值

我有 5 个不同长度的向量

a <- c(1) #with length of 1
b <- c(4.4,3.5) #length 2
c <- c(5.6,7.8,6.0) #length 3
d <- c(0.8,6.9,8.8,5.8) #length 4
e <- c(1.8,2.5,2.3,6.5,1.1) #length is 5
Run Code Online (Sandbox Code Playgroud)

我试图获取所有向量中元素的平均值:

 #since there are 5 values available for 1st element
 a[1]+b[1]+c[1]+d[1]+e[1] / 5 

 #since there are 4 values available for 2nd element
 b[2]+c[2]+d[2]+e[2] / 4 

#next divide by 3 and 2...1
c[3]+d[3]+e[3] / 3 and so on...
Run Code Online (Sandbox Code Playgroud)

我需要另一个数组中这些值的平均值,以便我可以进一步处理数据。

r data-manipulation mean

11
推荐指数
4
解决办法
486
查看次数

检查python字符串格式?

我有一堆字符串,但我只想保留这种格式:

x/x/xxxx xx:xx

检查字符串是否符合此格式的最简单方法是什么?(假设我想查看是否有2 /'和':')

python string data-manipulation

10
推荐指数
3
解决办法
4万
查看次数

从R中的字符串中提取字母

我有一个包含变量名称的字符向量,例如x <- c("AB.38.2", "GF.40.4", "ABC.34.2").我想提取字母,以便我有一个字符向量,现在只包含字母,例如c("AB", "GF", "ABC").

由于字母数量不同,我不能substring用来指定第一个和最后一个字符.

我怎么能这样做?

string r data-manipulation

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

根据列中的条件将值分配给组

我有一个如下所示的数据框:

> df = data.frame(group = c(1,1,1,2,2,2,3,3,3), 
                 date = c(1,2,3,4,5,6,7,8,9),
                 value = c(3,4,3,4,5,6,6,4,9))
> df
  group date value
1     1    1     3
2     1    2     4
3     1    3     3
4     2    4     4
5     2    5     5
6     2    6     6
7     3    7     6
8     3    8     4
9     3    9     9
Run Code Online (Sandbox Code Playgroud)

我想创建一个新列,其中包含与值列中的值"4"关联的每个组的日期值.

以下数据框显示了我希望实现的目标.

  group date value newValue
1     1    1     3        2
2     1    2     4        2
3     1    3     3        2
4     2    4     4        4 …
Run Code Online (Sandbox Code Playgroud)

r data-manipulation

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

将字符串转换为dict,然后访问键:值??? 如何访问Python的<class'dict'>中的数据?

我在访问字典中的数据时遇到问题.

Sys:Macbook 2012
Python:Python 3.5.1 :: Continuum Analytics,Inc.

我正在使用从csv创建的dask.dataframe.

编辑问题

我是如何达到这一点的

假设我从熊猫系列开始:

df.Coordinates
130      {u'type': u'Point', u'coordinates': [-43.30175...
278      {u'type': u'Point', u'coordinates': [-51.17913...
425      {u'type': u'Point', u'coordinates': [-43.17986...
440      {u'type': u'Point', u'coordinates': [-51.16376...
877      {u'type': u'Point', u'coordinates': [-43.17986...
1313     {u'type': u'Point', u'coordinates': [-49.72688...
1734     {u'type': u'Point', u'coordinates': [-43.57405...
1817     {u'type': u'Point', u'coordinates': [-43.77649...
1835     {u'type': u'Point', u'coordinates': [-43.17132...
2739     {u'type': u'Point', u'coordinates': [-43.19583...
2915     {u'type': u'Point', u'coordinates': [-43.17986...
3035     {u'type': u'Point', u'coordinates': [-51.01583...
3097     {u'type': u'Point', u'coordinates': [-43.17891... …
Run Code Online (Sandbox Code Playgroud)

python dictionary data-manipulation pandas dask

10
推荐指数
3
解决办法
5643
查看次数

R具有data.table的多列的多个统计信息

我希望获得与R中相同的结果,使用data.table汇总多个列,但需要几个汇总函数.

这是一个例子

data <- as.data.table(list(x1 = runif(200), x2 = 10*runif(200), group = factor(sample(letters[1:2]))))

res <- data[, rbindlist(lapply(.SD, function(x) {
              return(list(name = "varname", mean = mean(x), sd = sd(x)))
           }))
          , by = group, .SDcols = c("x1", "x2")
          ]
Run Code Online (Sandbox Code Playgroud)

并得到以下结果:

   group    name      mean        sd
1:     b varname 0.5755798 0.2723767
2:     b varname 5.5108886 2.7649262
3:     a varname 0.4906111 0.3060961
4:     a varname 4.7780189 2.9740149
Run Code Online (Sandbox Code Playgroud)

如何在第二列中获取列名('x1','x2')?我想我需要替换rbindlist其他东西,但是什么?有没有简单的解决方案?

r data-manipulation data.table

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

迭代地和分层地循环遍历行直到满足条件

我正在尝试解决R中的数据管理问题.

假设我的数据如下:

id <- c("123", "414", "606")
next.up <- c("414", "606", "119")
is.cond.met <- as.factor(c("FALSE", "FALSE", "TRUE"))
df <- data.frame(id, next.up, is.cond.met)

> df
   id next.up is.cond.met
1 123     414       FALSE
2 414     606       FALSE
3 606     119        TRUE
Run Code Online (Sandbox Code Playgroud)


我想获得的是以下内容:

id <- c("123", "414", "606")
next.up <- c("414", "606", "119")
is.cond.met <- as.factor(c("FALSE", "FALSE", "TRUE"))
origin <- c("606", "606", "119")
df.result <- data.frame(id, next.up, is.cond.met, origin)

> df.result
   id next.up is.cond.met origin
1 123     414       FALSE    606
2 414     606 …
Run Code Online (Sandbox Code Playgroud)

loops r data-manipulation dplyr tidyr

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

使用Python从数据框中删除多个列

我知道如何使用Python从数据框中删除列.但是对于我的问题,数据集很大,我想要删除的列被组合在一起,或者基本上是在列标题轴上单独展开.是否有一种更短的方法来切割或删除所有列的代码行数较少,而不是像我所做的那样将其写出来.我在这里的方式是有效的,但我想要一个更加总结的方式.

flight_data_copy_final是应该存储它的变量.

提前致谢

这是我的代码:

from IPython.display import display

flight_data_copy_version1 = flight_data_copy.drop(flight_data_copy.ix[:,"Year": "FlightDate"].columns, axis=1)
flight_data_copy_version2 = flight_data_copy_version1.drop("TailNum", axis=1)
flight_data_copy_version3 = flight_data_copy_version2.drop("OriginStateFips", axis=1)
flight_data_copy_version4 = flight_data_copy_version3.drop("DestStateFips", axis=1)
flight_data_copy_version5 = flight_data_copy_version4.drop("Diverted", axis=1)
flight_data_copy_version6 = flight_data_copy_version5.drop("Flights", axis=1)
flight_data_copy_final = flight_data_copy.drop(flight_data_copy_version6.ix[:,"FirstDepTime":].columns, axis=1)

print (display (flight_data_copy_final))
Run Code Online (Sandbox Code Playgroud)

python data-manipulation dataframe pandas

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

dplyr:如何以编程方式将full_join数据帧包含在列表列表中?

上下文和数据结构

我将与您分享我的庞大数据集的简化版本.这个简化版本完全尊重我原始数据集的结构,但包含的列表元素,数据框,变量和观察结果比原始数据集少.

根据对这个问题的最热烈回答:如何制作一个很好的R可重复的例子?,我使用输出共享我的数据集,通过dput(query1)在R控制台中复制/粘贴以下代码块,为您提供可立即在R中使用的内容:

       structure(list(plu = structure(list(year = structure(list(id = 1:3,
    station = 100:102, pluMean = c(0.509068994778059, 1.92866478959912,
    1.09517453602154), pluMax = c(0.0146962179957886, 0.802984389130343,
    2.48170762478472)), .Names = c("id", "station", "pluMean",
"pluMax"), row.names = c(NA, -3L), class = "data.frame"), month = structure(list(
    id = 1:3, station = 100:102, pluMean = c(0.66493845927034,
    -1.3559338786041, 0.195600637750077), pluMax = c(0.503424623872161,
    0.234402501255681, -0.440264545434053)), .Names = c("id",
"station", "pluMean", "pluMax"), row.names = c(NA, -3L), class = "data.frame"),
    week = structure(list(id = 1:3, station = 100:102, …
Run Code Online (Sandbox Code Playgroud)

r data-manipulation dplyr tidyverse

8
推荐指数
1
解决办法
768
查看次数

使用R替换列中的NAs和其他列的值

我不知道如何更换NA与其他列的值以s列中的R使用dplyr.MWE在下面.

Letters <- LETTERS[1:5]
Char    <- c("a", "b", NA, "d", NA)
df1 <- data.frame(Letters, Char)
df1

library(dplyr]

df1 %>%
  mutate(Char1 = ifelse(Char != NA, Char, Letters))

     Letters Char Char1
1       A    a    NA
2       B    b    NA
3       C <NA>    NA
4       D    d    NA
5       E <NA>    NA
Run Code Online (Sandbox Code Playgroud)

r data-manipulation dplyr

8
推荐指数
1
解决办法
2589
查看次数