同事,
我正在查看类似于以下摘录的数据框:
Month   Provider Items
January CofCom   25
july    CofCom   331
march   vobix    12
May     vobix    0
我想将每个单词的首字母大写,并降低每个单词的剩余字母.这将导致数据框类似于下面的数据框:
Month   Provider Items
January Cofcom   25
July    Cofcom   331
March   Vobix    12
May     Vobix    0
总之,我正在寻找R等同于MS Excel中可用的ROPER功能.
我试图弄清楚为什么我在使用ddply时收到错误消息.
示例数据:
data<-data.frame(area=rep(c("VA","OC","ES"),each=4),
    sex=rep(c("Male","Female"),each=2,times=3),
    year=rep(c(2009,2010),times=6),
    bin=c(110,120,125,125,110,130,125,80,90,90,80,140),
    shell_length=c(.4,4,1,2,.2,5,.4,4,.8,4,.3,4))
bin7<-ddply(data, .(area,year,sex,bin), summarize,n_bin=length(shell_length))
错误消息:.fun(piece,...)出错:缺少参数"by",没有默认值
我昨天收到此错误消息.我重新启动R并重新编写代码,一切都很好.今天早上我再次收到错误消息并重新启动R并没有解决问题.
我还尝试运行一些示例代码并得到相同的错误消息.
  # Summarize a dataset by two variables
require(plyr)
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)
)
# Note the use of the '.' function to allow
# group and sex to be used without quoting
ddply(dfx, .(group, sex), summarize,
 mean …我有一个小写的字符串向量.我想将它们改为标题案例,这意味着每个单词的第一个字母都会被大写.我已经设法用一个双循环来做,但我希望有一个更有效和优雅的方式来做到这一点,也许是一个单行gsub和一个正则表达式.
这里有一些示例数据,以及有效的双循环,其次是我尝试过的其他不起作用的东西.
strings = c("first phrase", "another phrase to convert",
            "and here's another one", "last-one")
# For each string in the strings vector, find the position of each 
#  instance of a space followed by a letter
matches = gregexpr("\\b[a-z]+", strings) 
# For each string in the strings vector, convert the first letter 
#  of each word to upper case
for (i in 1:length(strings)) {
  # Extract the position of each regex match for the string in row …我想将列中每个单词的第一个字母大写,而不将其余字母转换为小写。我正在尝试使用它,stringr因为它是矢量化的并且可以很好地与数据帧配合使用,但也会使用另一种解决方案。下面是一个表示,显示了我想要的输出和各种尝试。我只能选择第一个字母,但不知道如何将其大写。感谢您的帮助!
我还查看了相关帖子,但不确定如何在我的案例中应用这些解决方案(即在数据框中):
library(dplyr)
library(stringr)
words <-
  tribble(
    ~word, ~number,
    "problems", 99,
    "Answer", 42,
    "golden ratio", 1.61,
    "NOTHING", 0
  )
# Desired output
new_words <-
  tribble(
    ~word, ~number,
    "Problems", 99,
    "Answer", 42,
    "Golden Ratio", 1.61,
    "NOTHING", 0
  )
# Converts first letter of each word to upper and all other to lower
mutate(words, word = str_to_title(word))
#> # A tibble: 4 x 2
#>   word         number
#>   <chr>         <dbl>
#> 1 Problems      99   
#> 2 Answer        42 …我有一个包含多列的数据框,其中一列的城市名称首字母大写。我想查看此特定中的所有城市名称(带有小写字母)和包含所有现有列的更新数据框。
前任-
colA,colB,colC
Australia,Albany,23
Australia,Sydney,56
India,Delhi,67
India,Guntur,89
我想要输出为
colA,colB,colC
Australia,albany,23
Australia,sydney,56
India,delhi,67
India,guntur,89
我尝试使用 'dplyr::mutate_each' 函数,但结果如下
colB
albany   
sydney   
delhi    
guntur 
Names <- c("SUSAN,ALTOP","Brent,SPINER","KIM,YAMAGUCHI","John,McMurphy","Kevin,Y")
City <- c("Toronto","New York","Chicago","Toronto","Tokyo")
DF <- data.frame(Names,City)
我希望创建一个函数,该函数将在上面的简单示例数据框中使用大写的名字和姓氏,以便将名字读为“ Susan Altop”,“ Brent Spiner” ...等。(请注意,我也删除了逗号。)
我可以分别使用以下代码或通过管道使用以下代码来完成此操作。但是我希望创建一个函数,因为我必须做很多次,但是我不确定在使用dplyr,tidyr等时该如何做。我也愿意接受使用列表的更具创意的建议和purrr,如果可能的话。
DF <- DF %>% separate(DF,Names,c("First","Last",sep=","),remove=TRUE)
DF <- DF %>% mutate_each(funs(tolower),First,Last)
DF <- DF %>% mutate_each(funs(Capitalize),First,Last)
DF <- DF %>% mutate(NewNames=paste0(First," ",Last)