首页 \ 问答 \ 使用ifelse将新列添加到数据框。(Adding a new column to a data frame using ifelse. Why does a numeric value gets added?)

使用ifelse将新列添加到数据框。(Adding a new column to a data frame using ifelse. Why does a numeric value gets added?)

我正在尝试向数据框中添加一个新列,如下所示:

stateFrame$cgroup <- ifelse(stateFrame$Freq >= 11 & 
                   stateFrame$Freq < 83, "Other",stateFrame$city)

因此,如果行中的频率值介于11和83之间,则新列必须分配为Other ,否则应保持与另一列city (stateFrame$city)存在的值相同的值。

它适用于增加other值的值,但对于所有其他情况,对应于数据帧的行号分配如下:

在这里输入图像描述

这可能是什么原因? 我错过了什么?


I am trying to add a new column to a data frame as follows:

stateFrame$cgroup <- ifelse(stateFrame$Freq >= 11 & 
                   stateFrame$Freq < 83, "Other",stateFrame$city)

So, I want if the value of frequency in the row is between 11 and 83, the new column must be assigned Other, else it should keep the same value as exists in another column city (stateFrame$city).

It works fine for adding value as other but for all other cases, a row number corresponding to the data frame is assigned as shown below:

enter image description here

What could be the reason for this? Am I missing anything?


原文:https://stackoverflow.com/questions/48967296
更新时间:2021-06-05 21:06

最满意答案

我认为ifelse()中的“no”输入应该是一个向量,而你的是最有可能的因素。 使用as.vector(stateFrame$city) ,如下所示:

stateFrame$cgroup <- ifelse(stateFrame$Freq >= 11 & 
                   stateFrame$Freq < 83, "Other",as.vector(stateFrame$city))

编辑为了更精确,因子被存储为具有相关字符值的整数向量。 我认为整数是在你的代码中返回的。


I think your "no" input in ifelse() should be a vector and yours is most likely factor. Use as.vector(stateFrame$city) as follows:

stateFrame$cgroup <- ifelse(stateFrame$Freq >= 11 & 
                   stateFrame$Freq < 83, "Other",as.vector(stateFrame$city))

Edit To be more precise Factor is stored as vector of integers with related character values. I think integers are returned in your code.

相关问答

更多

从现有数据添加新列的ifelse语句错误(ifelse statement error for adding new column from existing data)

我们可以使用coalesce library(tidyverse) newdata %>% mutate(Station = coalesce(Tow.y, Tow)) # Tow.y Tow Station #1 21 NA 21 #2 NA NA NA #3 NA NA NA #4 NA 21 21 #5 22 NA 22 #6 NA NA NA #7 NA 22 ...

R - 将新列添加到数据框并复制现有行(R - Add a new column to the data frame and duplicate the existing rows)

使用data.table ,我们可以按ColA, ColB进行ColA, ColB 'year'的序列作为列进行分组 library(data.table) setDT(df1)[, .(Year = 2000:2002), .(ColA, ColB)] 或者tidyverse library(tidyr) crossing(df1, year = 2000:2002) # ColA ColB year #1 Asd 1 2000 #2 Asd 1 2001 #3 Asd ...

在向数据框添加列时嵌套IFELSE(Nested IFELSE in adding a column to a data frame)

这是嵌套的ifelse方法,但可能有更好的解决方案,特别是如果您的数据框架包含来自3个以上大陆的国家/地区。 df$Continent <- ifelse(df$Country=="India","Asia",ifelse(df$Country=="Mexico","North America","Europe")) This is the nested ifelse method, but there is probably a better solution, especially if y ...

使用ifelse将新列添加到数据框。(Adding a new column to a data frame using ifelse. Why does a numeric value gets added?)

我认为ifelse()中的“no”输入应该是一个向量,而你的是最有可能的因素。 使用as.vector(stateFrame$city) ,如下所示: stateFrame$cgroup <- ifelse(stateFrame$Freq >= 11 & stateFrame$Freq < 83, "Other",as.vector(stateFrame$city)) 编辑为了更精确,因子被存储为具有相关字符值的整数向量。 我认为整数是在你的代码中返回的。 ...

使用ifelse在R中对数据进行子集(Subsetting data in R with ifelse)

你不想要ifelse 。 你想要if和else 。 如果您有条件向量,则使用ifelse 。 您只有一个条件值。 myfunction = function(MyCondition="low"){ # special criteria lowRandomNumbers=c(58,61,64,69,73) highRandomNumbers=c(78,82,83,87,90) # subset the data based on MyCondition mydata <- if ...

使用重新编码将列添加到数据框(Adding a column to a data frame with recoding)

这是一个查找表,其中使用了一个已命名向量的名称来返回该向量内的值。 举几个例子: 作为提醒,可以通过引用索引或名称来对已命名向量进行子集化: names[1:2] AA AS "American" "Alaska" names[c("AA", "AS")] AA AS "American" "Alaska" 一个很好的功能是可以重复这些引用来生成扩展向量: names[rep(1:2, 2)] AA ...

将新列添加到数据框并使用特定逻辑的值填充行(Add new column to data frame and populate the row with value from a certain logic)

你使用ifelse在正确的轨道上,但是你可以在没有拆分DeptID情况下做到这DeptID : dat <- transform(dat, Rate = ifelse(DeptID == 2, ifelse(Active == 1, 0, 1), "")) dat > dat DeptID Name Age Active Rate 1 2 Aira 23 0 1 2 2 Ben 25 1 0 3 2 Cat 32 ...

将列添加到数据框,在其他列中测试分类变量(add column to data frame, testing categorical variable in other column)

如果你有一个非常大的数据框,那么data.table将对你非常有帮助。 以下作品: library(data.table) x <- c("dec 12", "jan 13", "feb 13", "march 13", "apr 13", "may 13", "june 13", "july 13", "aug 13", "sep 13", "oct 13", "nov 13") y <- c(234, 678, 534, 122, 179, 987, 872, 730, 295, 450 ...

相关文章

更多

最新问答

更多
  • 如何从远程文件拉取文件而不覆盖本地文件?(How do I pull files from remote without overwriting local files?)
  • Reactjs:状态改变时重新渲染iframe(Reactjs: re-renders iframes when state changed)
  • 奇怪的网址,以及跟随php页面流程的困难(odd url, and difficulty in following the php page flow)
  • 标签活动无效(Tab Activity is not working)
  • JavaME合适的语法编译器建议?(JavaME-suitable grammar compiler recommendations?)
  • 指定参数(Specifying arguments)
  • 可以通过Ruby插件或控制台覆盖Sketchup中的键盘快捷键吗?(Can one override keyboard shortcuts in Sketchup through the a Ruby Plugin or Console?)
  • 计算Java EE Web App中用户数的最佳方法(Best way to count number of users in a Java EE web App)
  • 无法使用templateUrl加载cordova中的外部模板(unable to load external templates in cordova with templateUrl)
  • PHPExcel:写入期间无法使用缓存(PHPExcel: Unable to use cache during write)
  • 在javascript中嵌套这个指针(nested this pointer in javascript)
  • 谁跟领航致远培训过,有问题问下啊
  • 控制器要求在入门时下载(Controller ask to download on entry)
  • 未能通过conda安装Asyncio(Failure to install Asyncio via conda)
  • 如何查找已完成项目的总长度?(How to find length of total completed items?)
  • 如何检查OleInitialize是否已被调用?(How to check if OleInitialize has already been called?)
  • SQL在特定范围内返回列中具有最大值的行(SQL Returning rows with max value in column, within a specific range)
  • preg_match从url获取id(preg_match get the id from url)
  • 如何在运算符中为make方程转换perl变量?(How to convert a perl variable in a operator for make equations?)
  • 在导航上方添加空格/标题。(Add a white space/ header above navigation.)
  • MeetingItem已保存;(MeetingItem saved; but change now shown in Calendar)
  • c#vb:我们应该使用System.Lazy进行资源密集型任务吗?(c# vb: Should we use System.Lazy for resource-intensive task? (when threading is not needed))
  • 为什么在armeabi代码中使用armeabi-v7a代码?(Why use armeabi-v7a code over armeabi code?)
  • 获取请求的自定义标头(Java HTTP)(Fetching a custom header of a request (Java HTTP))
  • 是否可以在嵌套的if语句中从varchar转换为numeric以动态评估参数?(Is it possible to convert from varchar to numeric within a nested if statement in order to dynamically evaluate a parameter?)
  • 如何将Html.ActionLink转换为链接到Ajax调用的按钮?(How to convert from Html.ActionLink to a button linked to Ajax call?)
  • 应用程序如何处理Windows符号链接?(How are Windows symbolic links treated by the apps?)
  • html,js,css在jsfiddle中工作,但不在sharepoint中(html, js, css works in jsfiddle but not in sharepoint)
  • 从Ruby脚本调用Elasticsearch Rest API(Calling Elasticsearch Rest API from Ruby script)
  • 如何将嵌套setTimeouts转换为承诺(How to convert nested setTimeouts to promises)