How can 2 strings be concatenated?

How can I merge/combine two value in R? For example I have: tmp = cbind("GAD", "AB") tmp # [,1] [,2] # [1,] "GAD" "AB" My goal is to get tmp as one string tmp_new = "GAD,AB" Which Function can do this for me? paste() is the way to go. As the previous posters pointed out, paste can do two things: concatenate values into one "string", eg > paste("Hello", "world", sep="

2个字符串如何连接?

我如何合并/合并R中的两个值? 例如,我有: tmp = cbind("GAD", "AB") tmp # [,1] [,2] # [1,] "GAD" "AB" 我的目标是将tmp作为一个字符串 tmp_new = "GAD,AB" 哪个功能可以为我做这个? paste() 是要走的路。 正如之前的海报所指出的,粘贴可以做两件事: 将值连接成一个“字符串”,例如 > paste("Hello", "world", sep=" ") [1] "Hello world" 其中参数sep指定要连接的参数之间要使用的字符,或折叠字符

Concatenate a vector of strings/character

If I have a vector of type character, how can I concatenate the values into string? Here's how I would do it with paste(): sdata = c('a', 'b', 'c') paste(sdata[1], sdata[2], sdata[3], sep ='') yielding "abc" . But of course, that only works if I know the length of sdata ahead of time. Try using an empty collapse argument within the paste function: paste(sdata, collapse = &#

连接字符串/字符的向量

如果我有一个字符类型的向量,我如何将这些值连接成字符串? 这是我用paste()做的方法: sdata = c('a', 'b', 'c') paste(sdata[1], sdata[2], sdata[3], sep ='') 产生"abc" 。 但是,当然,只有在提前了解sdata的长度时才有效。 尝试在粘贴功能中使用空的折叠参数: paste(sdata, collapse = '') 感谢http://twitter.com/onelinetips/status/7491806343 马特的回答绝对是正确的答案。 不过

Boxplot for each individual level in two factor columns?

This is my code. I have two factor variables ( cyl and am )that I want to plot in my box plot. boxplot(mpg~cyl+am,data=mtcars) I want to make a boxplot for each individual factor levels - 4,6,8,0,1 but not the interaction of them (eg 4.0). My box plot should only have (4,6,8,0,1) on the axis. How do I do that? You may reshape the data and then you can do the plotting, However this may n

Boxplot为两个因子列中的每个单独级别?

这是我的代码。 我有两个因子变量( cyl和am ),我想在我的箱形图中绘制。 箱线图(MPG〜CYL +上午,数据= mtcars) 我想为每个单独的因素水平做一个箱形图 - 4,6,8,0,1但不是它们的相互作用(例如4.0)。 我的盒子图应该只有(4,6,8,0,1)在轴上。 我怎么做? 你可以重新塑造数据,然后你可以做绘图,但这可能不是优雅的 library(reshape2) mtcars_subset <- mtcars[,c("am", "cyl", "mpg")] dat <- melt(mtc

Calculate mean of calculated values

I want to calculate the mean of the resulting values returned by abs(((column A - column B)/column A)*100) So for example on mtcars data i try: > mtcars mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8

计算计算值的平均值

我想计算由abs(((column A - column B)/column A)*100)返回的结果值的平均值。 因此,例如在我尝试mtcars数据: > mtcars mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1

Error calculating mean by input variable in Shiny

Using the mtcars dataset in R, I am tying to use the input variable (cyl, am etc...) to calculate the mean mpg by that variable. My code in the ui.R is like: verbatimTextOutput("Avg_Mileage") My code in the shinServer function in server.R is like: carsdata <- mtcars output$Avg_Mileage <- renderPrint({aggregate(mpg~input$variable, carsdata,mean)}) I have tried to change the code in se

在Shiny中通过输入变量计算平均值的错误

使用R中的mtcars数据集,我正在使用输入变量(cyl,am等)来计算该变量的平均mpg。 我在ui.R中的代码如下所示: verbatimTextOutput("Avg_Mileage") 我在server.R中的shinServer函数中的代码如下所示: carsdata <- mtcars output$Avg_Mileage <- renderPrint({aggregate(mpg~input$variable, carsdata,mean)}) 我试图以各种方式更改server.R中的代码。 但我不断收到有关冲突变量长度的消息: “变量长度不同(找到

Predict values from multivariate linear model

I need help predicting a value from new data, from a multivariate lm model. project_model <- lm(project_data$Log.Odds.Ratio ~ project_data$Complexity.Level + project_data$Product.Type + project_data$Plant.Normalized.Hours + project_data$Norm.Sq.Ft) (Four predictors) I want to predict the Log.Odds.Ratio from a new set of data. (Same column names) new_data <- data.frame(Complexity.Level

预测多元线性模型的值

我需要帮助从多元lm模型中预测新数据的价值。 project_model <- lm(project_data$Log.Odds.Ratio ~ project_data$Complexity.Level + project_data$Product.Type + project_data$Plant.Normalized.Hours + project_data$Norm.Sq.Ft) (四个预测指标) 我想从一组新数据中预测Log.Odds.Ratio。 (相同的列名称) new_data <- data.frame(Complexity.Level = 3,Product.Type = "End",Plant.Normalized.Hours = 1.5 ,Nor

Using dplyr window functions to calculate percentiles

I have a working solution but am looking for a cleaner, more readable solution that perhaps takes advantage of some of the newer dplyr window functions. Using the mtcars dataset, if I want to look at the 25th, 50th, 75th percentiles and the mean and count of miles per gallon ("mpg") by the number of cylinders ("cyl"), I use the following code: library(dplyr) library(tidyr)

使用dplyr窗口函数来计算百分位数

我有一个可行的解决方案,但我正在寻找更清晰,更易读的解决方案,可能会利用一些较新的dplyr窗口功能。 使用mtcars数据集,如果我想查看第25,50和75百分位数以及汽缸数(“cyl”)的每加仑英里数(“mpg”)的平均值和计数,我使用以下代码: library(dplyr) library(tidyr) # load data data("mtcars") # Percentiles used in calculation p <- c(.25,.5,.75) # old dplyr solution mtcars %>% group_by(cyl) %>%

How to make a reproducible example of a database connection?

From time to time I need to make reproducible examples about errors I get when querying a database; sometimes these errors cannot be reproduced using the built-in datasets. Is there any list of publicly available databases that we can use to make reproducible examples? I knew about his one src_mysql(dbname = "dplyr", host = "dplyr.csrrinzqubik.us-east-1.rds.amazonaws.com",

如何制作一个可重复的数据库连接示例?

有时我需要对查询数据库时遇到的错误进行可重复的示例; 有时这些错误不能使用内置数据集进行重现。 是否有任何公开可用的数据库列表,我们可以使用它们来制作可重复的示例? 我知道他的一个 src_mysql(dbname = "dplyr", host = "dplyr.csrrinzqubik.us-east-1.rds.amazonaws.com", port = 3306, user = "dplyr", password = "dplyr") 但它给了

Quickly reproduce data

I am new-ish to SO and I am curious how you quickly read in data from the questions people post. When someone posts an example data set that looks like this: x=rnorm(100,0,1) y=rnorm(100,0,1) d=cbind(x,y) I can quickly reproduce it in R. However, I often see people post example data that looks like: df a b c d e f g h i j k l m n o 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0

快速重现数据

我很新奇,我很好奇你如何快速阅读人们发布的问题的数据。 当有人发布如下所示的示例数据集时: x=rnorm(100,0,1) y=rnorm(100,0,1) d=cbind(x,y) 我可以在R中快速重现它。但是,我经常看到人们发布的示例数据如下所示: df a b c d e f g h i j k l m n o 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 4 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 5 0 1 0 0 1 1 0 0 0 1

How to easily combine data sets; how to quantify text data

I'm just getting started with R and R-Studio. I'm working with a couple different data sets: each contains the same variables, and within those variables the same types of information. The data sets have been imported into R-Studio as separate sets/files. First question: how can I go about combining them? There are seventeen in all. Here is an abbreviated example of two of them:

如何轻松组合数据; 如何量化文本数据

我刚刚开始使用R和R-Studio。 我正在处理几个不同的数据集:每个数据集都包含相同的变量,并且在这些变量中包含相同类型的信息。 数据集已作为单独的集/文件导入到R-Studio中。 第一个问题:我怎么能把它们合并起来? 共有十七个。 以下是其中两个的缩写示例: EVENT_ID STATE YEAR MONTH_NAME EVENT_TYPE INJURIES_DIRECT DEATHS_DIRECT 1 5551758 MASSACHUSETTS 1996 January Heavy Snow