在R中添加一列的字符值 [英] Adding character values of a column in R

查看:800
本文介绍了在R中添加一列的字符值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两列,即square_id& Smart_Nsmart如下所示。
我想对每个square_id和ggplot计算(添加)N和S的数据,即绘制square_id和Smart_Nsmart。

square_id 1
1
2 2 2 2 3 3 3 3
Smart_Nsmart
SNNNSSNSSS

解决方案

我们可以使用 count ,然后使用 ggplot 来绘制频率。在这里,我们正在用 geom_bar 来绘制它(因为从OP的帖子中不清楚)

  library(dplyr)
library(ggplot2)
df%>%
count(square_id,Smart_Nsmart)%>%
ggplot(。,aes (x = square_id,y = n,fill = Smart_Nsmart))+
geom_bar(stat ='identity')


I have two columns i.e. square_id & Smart_Nsmart as given below. I want to count(add) N's and S's against each square_id and ggplot the data i.e. plot square_id vs Smart_Nsmart.

square_id 1 1 2 2 2 2 3 3 3 3 Smart_Nsmart S N N N S S N S S S

解决方案

We can use count and then use ggplot to plot the frequency. Here, we are plotting it with geom_bar (as it is not clear from the OP's post)

library(dplyr)
library(ggplot2)
df %>% 
   count(square_id, Smart_Nsmart) %>%
   ggplot(., aes(x= square_id, y = n, fill = Smart_Nsmart)) + 
         geom_bar(stat = 'identity')

这篇关于在R中添加一列的字符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆