R 从一列中分离百万和十亿数据
我正在尝试下面的代码,用于将“M”和“B”与其值分隔在两个不同的列中 我想要这样的输出:R 从一列中分离百万和十亿数据,r,R,我正在尝试下面的代码,用于将“M”和“B”与其值分隔在两个不同的列中 我想要这样的输出: level 1 level 2 M 3.2 B 3.6 M 4 B 2.8 B 3.5 输入: reve=c("M 3.2","B 3.6","B 2.8","B 3.5","M 4") #class(reve) data=data.frame(reve) 这是我试过的 index=which(grepl("M ",data$reve) data$re
level 1 level 2
M 3.2 B 3.6
M 4 B 2.8
B 3.5
输入:
reve=c("M 3.2","B 3.6","B 2.8","B 3.5","M 4")
#class(reve)
data=data.frame(reve)
这是我试过的
index=which(grepl("M ",data$reve)
data$reve=gsub("M ","",data$reve)
data$reve=gsub("B ","",data$reve)
data$reve=as.numeric(data$reve)
下面是一个
baser
方法
lst <- split(reve, substr(reve, 1, 1))
df1 <- as.data.frame(lapply(lst, `length<-`, max(lengths(lst))))
df1
# B M
#1 B 3.6 M 3.2
#2 B 2.8 M 4
#3 B 3.5 <NA>
如果我误解了您想要的输出,请尝试
df1 <- data.frame(do.call(rbind, (strsplit(reve, " "))), stringsAsFactors = FALSE)
df1[] <- lapply(df1, type.convert, as.is = TRUE)
df1
# X1 X2
#1 M 3.2
#2 B 3.6
#3 B 2.8
#4 B 3.5
#5 M 4.0
df1如果您有一个数据帧,那么可以使用dplyr separate()实现
我举一个例子:
library(dplyr)
df <- tibble(coupe = c("M 2.3", "M 4.5", "B 1"))
df %>% separate(coupe, c("MorB","Quant"), " ")
我们可以将百万或数十亿计算如下:
输入数据集:
reve=c("M 3.2","B 3.6","B 2.8","B 3.5","M 4")
data=data.frame(reve)
代码
library(dplyr)
library(tidyr)
data %>%
separate(reve, c("Label", "Value"),extra = "merge") %>%
group_by(Label) %>%
summarise(n = n())
输出
# A tibble: 2 x 2
Label n
<chr> <int>
1 B 3
2 M 2
#一个tible:2x2
标签n
1 B 3
2平方米
我认为基于regex的选项也可能有助于解决这些类型的问题
reve=c("M 3.2","B 3.6","B 2.8","B 3.5","M 4")
data=data.frame(reve, stringsAsFactors = F) # handle your data as strings, not factors
# regex to extract M vals and B vals
mvals <- stringi::stri_extract_all_regex(data, "M+\\s[0-9]\\.[0-9]|M+\\s[0-9]")[[1]]
bvals <- stringi::stri_extract_all_regex(data, "B+\\s[0-9]\\.[0-9]|B+\\s[0-9]")[[1]]
# gluing things together into a single df
len <- max(length(mvals), length(bvals)) # find the length
data.frame(M = c(mvals, rep(NA, len - length(mvals))) # ensure vectors are the same size
,B = c(bvals, rep(NA, len - length(bvals)))) # ensure vectors are the same size
reve=c(“M3.2”、“B3.6”、“B2.8”、“B3.5”、“M4”)
data=data.frame(reve,stringsAsFactors=F)#将数据作为字符串而不是因子处理
#用于提取M VAL和B VAL的正则表达式
mvals计算本栏中记录的观测值,单位为百万美元。公司广告费12亿美元340亿美元560万美元420万美元970万美元公司广告费是数据框中的一列,我想以百万美元计算行数。@Kalyani:你能告诉我这个问题的预期产出吗?我看不清楚。我想要的行数有数百万美元。假设公司广告费为12亿美元,34B美元,560万美元,70亿美元,970万美元,910万美元,那么我想要的是价值数百万美元的行。在这种情况下,它是560万美元970万美元910万美元,计数是=3。@Kalyani:我已经更新了答案。请检查,并让我知道的情况下的问题。我已经尝试了这个代码,但单独的代码给出错误。我想知道这个问题的答案:数一数本专栏中有多少观察结果是以百万美元记录的。公司广告12亿美元34B亿美元560万美元720万美元970万美元公司广告是数据框中的一列,我想以百万美元计算行数。df%>%分开(YourColumn,c(“MorB”,“Quant”),“”)%%>%过滤器(MorB==“M”)%%>%nrow()当我运行上述代码时,我得到了错误:函数_list[[I]](值)中的错误:找不到函数“separate”df%separate(公司_广告,c(“MorB”,“Quant”),“”)%%>%filter(MorB==“M”)%%>%nrow(),在此之后我得到错误:函数_list[[I]](值):找不到函数“Seperat”人工写入库(dplyr)
library(dplyr)
library(tidyr)
data %>%
separate(reve, c("Label", "Value"),extra = "merge") %>%
group_by(Label) %>%
summarise(n = n())
# A tibble: 2 x 2
Label n
<chr> <int>
1 B 3
2 M 2
reve=c("M 3.2","B 3.6","B 2.8","B 3.5","M 4")
data=data.frame(reve, stringsAsFactors = F) # handle your data as strings, not factors
# regex to extract M vals and B vals
mvals <- stringi::stri_extract_all_regex(data, "M+\\s[0-9]\\.[0-9]|M+\\s[0-9]")[[1]]
bvals <- stringi::stri_extract_all_regex(data, "B+\\s[0-9]\\.[0-9]|B+\\s[0-9]")[[1]]
# gluing things together into a single df
len <- max(length(mvals), length(bvals)) # find the length
data.frame(M = c(mvals, rep(NA, len - length(mvals))) # ensure vectors are the same size
,B = c(bvals, rep(NA, len - length(bvals)))) # ensure vectors are the same size