我有以下数据表:
dt <- fread("
ID | EO_1 | EO_2 | EO_3 | GROUP
ID_001 | 0.5 | 1.2 | | A
ID_002 | | | | A
ID_003 | | | | A
ID_004 | | | | A
ID_001 | 0.4 | 2.5 | | B
ID_002 | | | | B
ID_003 | | | | B
ID_004 | | | | B
",
sep = "|",
colClasses = c("character", "numeric", "numeric", "numeric", "character"))
Run Code Online (Sandbox Code Playgroud)
我正在尝试执行一些逐行操作,这些操作有时取决于前一行的数据。进一步来说:
calc_EO_1 <- function( …Run Code Online (Sandbox Code Playgroud) 我不知道它是否已被问过,因为它似乎应该是一个常见的问题,但即使我尝试过,我仍然无法找到任何关于它的信息.对不起,那个案子.
鉴于dt <- data.table(col1 = c(1, 2, 3, 4), col2 = c("a", "b", "c", "d"), col3 = c(T, F, T, F)):
dt[, c("col1", "col2")]dt[, -c("col1", "col2")]dt[, "col1" := NULL]dt[, c("col1", "col2") := NULL]dt[, -"col1" := NULL]dt[, -c("col1", "col2") := NULL]我很确定它必须以任何方式实现最后两个,但对我而言,目前还不可能.你能给我一些建议吗?我不是编程的新手,我知道一点R(不过我最强的),但我对data.table相当新.
感谢大家.
编辑:这个问题在以下链接中有一个答案,虽然主题没有解决这个问题所以如果你正在寻找这个特殊的疑问很难找到:
我使用的是 UCI 的成人年收入。
我有一个数据框,其中一列中有一个分类变量,我想将其分组为不同的类别(一些常见的特征工程)。
df.groupBy('education').count().show()
Run Code Online (Sandbox Code Playgroud)
给出:
+------------+-----+
| education|count|
+------------+-----+
| 10th| 1223|
| Masters| 2514|
| 5th-6th| 449|
| Assoc-acdm| 1507|
| Assoc-voc| 1959|
| 7th-8th| 823|
| 9th| 676|
| HS-grad|14783|
| Bachelors| 7570|
| 11th| 1619|
| 1st-4th| 222|
| Preschool| 72|
| 12th| 577|
| Doctorate| 544|
|Some-college| 9899|
| Prof-school| 785|
+------------+-----+
Run Code Online (Sandbox Code Playgroud)
我想将以下类别放入特定组中,这样:
dropout = ['Preschool', '1st-4th', '5th-6th', '7th-8th', '9th', '10th', '11th', '12th']
community_college = ['Assoc-acdm', 'Assoc-voc', 'Some-college']
masters = ['Prof-school']
Run Code Online (Sandbox Code Playgroud)
为此,我可以执行以下操作:
from pyspark.sql.functions import …Run Code Online (Sandbox Code Playgroud)