我有这样的角色矢量:
a <- c("a,b,c", "a,b", "a,b,c,d")
我想要做的是创建一个如下所示的数据框:
a b c d
1] 1 1 1 0
2] 1 1 0 0
3] 1 1 1 1
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,我需要使用的某种组合read.table和reshape,但我真的很挣扎.任何和帮助赞赏.
我看了但是没有找到如何在R中的字符串向量中的每个元素的末尾添加字符的答案,除了最后一个 ...
考虑以下:
data <- c("cat", "dog", "mouse", "lion")
Run Code Online (Sandbox Code Playgroud)
我想在每个元素的末尾应用一个粘贴","的函数,结果是:
[1] "cat,", "dog,", "mouse,", "lion"
Run Code Online (Sandbox Code Playgroud)
应用功能?for loop?任何帮助表示赞赏......
我有以下data.frame:
set.seed(126)
df <- data.frame(a=sample(c(1:100, NA), 10), b=sample(1:100, 10), c=sample(1:100, 10), d = c(1:10))
a b c d
1 18 27 53 1
2 44 16 66 2
3 58 47 3 3
...
Run Code Online (Sandbox Code Playgroud)
以下查找表:
varnames <- data.frame(old = c("a", "b", "c"), new = c("dog", "cat", "mouse"))
old new
1 a dog
2 b cat
3 c mouse
Run Code Online (Sandbox Code Playgroud)
我想要做的是替换names(df)相应的varnames$new...如果a names(df)不在varnames$old,则保留在df中的colname ...
我想要返回的结果data.frame看起来像这样:
dog cat mouse d
1 57 10 83 1
2 53 …Run Code Online (Sandbox Code Playgroud) 想象一下数据帧:
set.seed(1234)
data<-data.frame(id = sample(letters, 26, replace = FALSE),
a = sample(1:10,26,replace=T),
b = sample(1:10,26,replace=T),
c = sample(1:10,26,replace=T))
Run Code Online (Sandbox Code Playgroud)
我想为每个保留id最大值所在的列名.
我正在寻找的结果是一个尺寸为26 x 2的数据框,其中列为for id列largest_value_var.将largest_value_var要包含下面 a,b或c.
到目前为止,我已经能够使用以下方法提取与最大值关联的变量名称:
apply(data[,-1], 1, function(x) c(names(x))[which.max(x)])
Run Code Online (Sandbox Code Playgroud)
但我似乎无法将我想要的结果变成数据帧...任何帮助都表示赞赏.
我有这样一个data.frame:
df <- data.frame(a = runif(1000), b = runif(1000), c = runif(1000), d = sample(c("yes", "no"), 1000, replace=TRUE))
Run Code Online (Sandbox Code Playgroud)
我进行了逻辑回归:
lm <- glm(data = df, factor(d) ~ a + b + c, family = binomial)
Run Code Online (Sandbox Code Playgroud)
产生了预测的概率:
df$pred <- predict(lm, type = "response")
Run Code Online (Sandbox Code Playgroud)
并希望图表既具有抖动积为结果a和b,颜色为填充d和光滑的线(使用geom_smooth),用于简单地作用a于d
我已经试过了:
ggplot(data = df , aes(x=a, y = b, color = factor(d))) + geom_jitter() +
geom_smooth(data = df, aes(x=a, y = pred))
Run Code Online (Sandbox Code Playgroud)
但这并没有产生我想要的。我想要的是这条线:
ggplot(data = df , aes(x=a, y …Run Code Online (Sandbox Code Playgroud) 我是一个相对较新的python用户,使用我的IDE(pycharm)得到一个时髦的错误,但是在使用cmd行时没有.
我只是:
path ='C:\Users\Dell\Downloads\users.dat'
import pandas as pd
unames = ['user_id', 'gender', 'age', 'occupation', 'zip']
users = pd.read_table(path, sep='::', header=None, names=unames)
Run Code Online (Sandbox Code Playgroud)
之后我收到一个错误,表明:
ParserWarning: Falling back to the 'python' engine because the 'c' engine does not
support regex separators; you can avoid this warning by specifying engine='python'.
ParserWarning)
Run Code Online (Sandbox Code Playgroud)
当我将相同的命令输入到cmd行并按users预期打印数据时(即没有错误或任何时髦).
编辑:同样当我输入
ratingsdata ='C:\Users\Dell\Downloads\ratings.dat'
Run Code Online (Sandbox Code Playgroud)
我得到一个时髦IOError: [Errno 22] invalid mode ('r') or filename:不确定为什么/r在文件路径中不正常...我理解它是正则表达式,但在引用行内?
救命!
我的问题可能是双重的:我想,对于特定条件和某个特定群体,我要归咎于两组价值观.
让我用数据解释一下:
df <- data.frame(id = c(rep("a",6),rep("b",6),rep("c",7)),
num = c(rep(1:6, 2),rep(1:7)),
status = c(rep("good",6),rep("bad",6),rep("bad",7)), value = c(100))
> df
id num status value
1 a 1 good 100
2 a 2 good 100
3 a 3 good 100
4 a 4 good 100
5 a 5 good 100
6 a 6 good 100
7 b 1 bad 100
8 b 2 bad 100
9 b 3 bad 100
10 b 4 bad 100
11 b 5 bad 100
12 b …Run Code Online (Sandbox Code Playgroud) 如果已经回答,请原谅我,但使用搜索工具或基本的谷歌查询找不到它.
我试图返回一个值,该值指示SQL中列中任何不同值的最大行数.
例如,我想使用类似的东西
SELECT MAX(COUNT(DISTINCT person_id) AS MAX_NUM_PERS_ROW
FROM mytable
Run Code Online (Sandbox Code Playgroud)
如果表中行数最多的人有5行,则返回的值为5 ...
任何和所有的帮助表示赞赏!
我正在尝试创建一个列表或df,其中包含数据帧中所有因素的因子级别数.
所以,看来我需要首先确定因素(使用is.factor()),然后计算每个因素的数量(使用length())
我正在使用sapply,但无法得到我想要的东西.
任何帮助,将不胜感激.
这是我到目前为止所做的:
fac <- sapply(cf_nm, function(x) is.factor(x) )
fac <- cf_nm[fac]
Run Code Online (Sandbox Code Playgroud)
我可以简单地计算这里的水平 - 但我希望有一种更有说服力的方式来做我想做的事情.
我有一个日期框架:
long <- data.frame(subj = c(1,1,2,2,2), code = c("a", "b", "a", "d", "e"))
subj code
1 1 a
2 1 b
3 2 a
4 2 d
5 2 e
Run Code Online (Sandbox Code Playgroud)
我似乎无法在这样的数据中构造数据,code每个数据重复3次subj,每次code.n都是按时间顺序排列的代码.
因此:
subj code.1 code.2 code.3
1 1 a b <NA>
2 2 a d e
Run Code Online (Sandbox Code Playgroud)
任何和所有帮助表示赞赏.
使用dplyr创建总相对频率表并按两个变量分组时,我得到了意想不到的结果。下面是一个例子:
set.seed(1234)
dat1 = data.frame(
color = c(c(rep("red", 4), rep("green", 4))),
type = c(c(rep(c(
"big", "small"
), 4))),
value = sample(1:6, 8, replace = T)
)
dat1 %>% group_by(color, type) %>% summarise(n = n()) %>%
mutate(total = sum(n), rel.freq = n / total)
Run Code Online (Sandbox Code Playgroud)
以下是上述代码的结果:
# A tibble: 4 x 5
# Groups: color [2]
color type n total rel.freq
<fct> <fct> <int> <int> <dbl>
1 green big 2 4 0.500
2 green small 2 4 0.500
3 red …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的data.frame:
set.seed(126)
df <- data.frame(a=sample(c(1:100, NA), 10), b=sample(1:100, 10), c=sample(1:100, 10))
a b c
1 65 48 19
2 46 15 80
3 NA 47 84
4 68 34 46
5 23 75 42
6 92 87 68
7 79 28 48
8 84 55 9
9 28 43 38
10 94 99 77
>
Run Code Online (Sandbox Code Playgroud)
我想写一个函数,将所有列中的所有值转换为NA,如果df$a是,NA但是,我不想只分配b和c值NA,而是我想要一个函数将data.frame中的所有列转换为如果条件is.na(a)满足则为NA ,无论列数如何.